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 | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 7 | * |
| 8 | * 14 Nov 2000 ht - Changed the name of function xmlBufferWriteChar under VMS |
| 9 | * as it was similar to xmlBufferWriteCHAR when compiling without case |
| 10 | * sensitivity. |
| 11 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 15 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 16 | #else |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 17 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | #include <string.h> /* for memset() only ! */ |
| 22 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 23 | #ifdef HAVE_CTYPE_H |
| 24 | #include <ctype.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_STDLIB_H |
| 27 | #include <stdlib.h> |
| 28 | #endif |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 29 | #ifdef HAVE_ZLIB_H |
| 30 | #include <zlib.h> |
| 31 | #endif |
| 32 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 33 | #include <libxml/xmlmemory.h> |
| 34 | #include <libxml/tree.h> |
| 35 | #include <libxml/parser.h> |
| 36 | #include <libxml/entities.h> |
| 37 | #include <libxml/valid.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 38 | #include <libxml/xmlerror.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 40 | static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 41 | static xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 42 | int oldXMLWDcompatibility = 0; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 43 | int xmlIndentTreeOutput = 0; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 44 | xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 45 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 46 | static int xmlCompressMode = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 47 | static int xmlCheckDTD = 1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 48 | int xmlSaveNoEmptyTags = 0; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 49 | |
| 50 | #define IS_BLANK(c) \ |
| 51 | (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' ')) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 52 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 53 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 54 | xmlNodePtr ulccur = (n)->children; \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 55 | if (ulccur == NULL) { \ |
| 56 | (n)->last = NULL; \ |
| 57 | } else { \ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 58 | while (ulccur->next != NULL) { \ |
| 59 | ulccur->parent = (n); \ |
| 60 | ulccur = ulccur->next; \ |
| 61 | } \ |
| 62 | ulccur->parent = (n); \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 63 | (n)->last = ulccur; \ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 64 | }} |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 65 | |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 66 | /* #define DEBUG_BUFFER */ |
| 67 | /* #define DEBUG_TREE */ |
| 68 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 69 | /************************************************************************ |
| 70 | * * |
| 71 | * Allocation and deallocation of basic structures * |
| 72 | * * |
| 73 | ************************************************************************/ |
| 74 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 75 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 76 | * xmlSetBufferAllocationScheme: |
| 77 | * @scheme: allocation method to use |
| 78 | * |
| 79 | * Set the buffer allocation method. Types are |
| 80 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 81 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 82 | * improves performance |
| 83 | */ |
| 84 | void |
| 85 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 86 | xmlBufferAllocScheme = scheme; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * xmlGetBufferAllocationScheme: |
| 91 | * |
| 92 | * Types are |
| 93 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 94 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 95 | * improves performance |
| 96 | * |
| 97 | * Returns the current allocation scheme |
| 98 | */ |
| 99 | xmlBufferAllocationScheme |
| 100 | xmlGetBufferAllocationScheme() { |
| 101 | return xmlBufferAllocScheme; |
| 102 | } |
| 103 | |
| 104 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 105 | * xmlNewNs: |
| 106 | * @node: the element carrying the namespace |
| 107 | * @href: the URI associated |
| 108 | * @prefix: the prefix for the namespace |
| 109 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 110 | * Creation of a new Namespace. This function will refuse to create |
| 111 | * a namespace with a similar prefix than an existing one present on this |
| 112 | * node. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 113 | * We use href==NULL in the case of an element creation where the namespace |
| 114 | * was not defined. |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 115 | * Returns returns a new namespace pointer or NULL |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 116 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 117 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 118 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 119 | xmlNsPtr cur; |
| 120 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 121 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 122 | * Allocate a new Namespace and fill the fields. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 123 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 124 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 125 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 126 | xmlGenericError(xmlGenericErrorContext, |
| 127 | "xmlNewNs : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 128 | return(NULL); |
| 129 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 130 | memset(cur, 0, sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 131 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 132 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 133 | if (href != NULL) |
| 134 | cur->href = xmlStrdup(href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 135 | if (prefix != NULL) |
| 136 | cur->prefix = xmlStrdup(prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * Add it at the end to preserve parsing order ... |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 140 | * and checks for existing use of the prefix |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 141 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 142 | if (node != NULL) { |
| 143 | if (node->nsDef == NULL) { |
| 144 | node->nsDef = cur; |
| 145 | } else { |
| 146 | xmlNsPtr prev = node->nsDef; |
| 147 | |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 148 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 149 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 150 | xmlFreeNs(cur); |
| 151 | return(NULL); |
| 152 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 153 | while (prev->next != NULL) { |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 154 | prev = prev->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 155 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 156 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 157 | xmlFreeNs(cur); |
| 158 | return(NULL); |
| 159 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 160 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 161 | prev->next = cur; |
| 162 | } |
| 163 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 164 | return(cur); |
| 165 | } |
| 166 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 167 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 168 | * xmlSetNs: |
| 169 | * @node: a node in the document |
| 170 | * @ns: a namespace pointer |
| 171 | * |
| 172 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 173 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 174 | void |
| 175 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 176 | if (node == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 177 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 178 | xmlGenericError(xmlGenericErrorContext, |
| 179 | "xmlSetNs: node == NULL\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 180 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
| 183 | node->ns = ns; |
| 184 | } |
| 185 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 186 | /** |
| 187 | * xmlFreeNs: |
| 188 | * @cur: the namespace pointer |
| 189 | * |
| 190 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 191 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 192 | void |
| 193 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 194 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 195 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 196 | xmlGenericError(xmlGenericErrorContext, |
| 197 | "xmlFreeNs : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 198 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 199 | return; |
| 200 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 201 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 202 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 203 | memset(cur, -1, sizeof(xmlNs)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 204 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 207 | /** |
| 208 | * xmlFreeNsList: |
| 209 | * @cur: the first namespace pointer |
| 210 | * |
| 211 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 212 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 213 | void |
| 214 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 215 | xmlNsPtr next; |
| 216 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 217 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 218 | xmlGenericError(xmlGenericErrorContext, |
| 219 | "xmlFreeNsList : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 220 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | while (cur != NULL) { |
| 224 | next = cur->next; |
| 225 | xmlFreeNs(cur); |
| 226 | cur = next; |
| 227 | } |
| 228 | } |
| 229 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 230 | /** |
| 231 | * xmlNewDtd: |
| 232 | * @doc: the document pointer |
| 233 | * @name: the DTD name |
| 234 | * @ExternalID: the external ID |
| 235 | * @SystemID: the system ID |
| 236 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 237 | * Creation of a new DTD for the external subset. To create an |
| 238 | * internal subset, use xmlCreateIntSubset(). |
| 239 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 240 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 241 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 242 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 243 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 244 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 245 | xmlDtdPtr cur; |
| 246 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 247 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 248 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 249 | xmlGenericError(xmlGenericErrorContext, |
| 250 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 251 | /* !!! */ (char *) name, doc->name, |
| 252 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 253 | #endif |
| 254 | return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Allocate a new DTD and fill the fields. |
| 259 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 260 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 261 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 262 | xmlGenericError(xmlGenericErrorContext, |
| 263 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 264 | return(NULL); |
| 265 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 266 | memset(cur, 0 , sizeof(xmlDtd)); |
| 267 | cur->type = XML_DTD_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 268 | |
| 269 | if (name != NULL) |
| 270 | cur->name = xmlStrdup(name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 271 | if (ExternalID != NULL) |
| 272 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 273 | if (SystemID != NULL) |
| 274 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 275 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 276 | doc->extSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 277 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 278 | |
| 279 | return(cur); |
| 280 | } |
| 281 | |
| 282 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 283 | * xmlGetIntSubset: |
| 284 | * @doc: the document pointer |
| 285 | * |
| 286 | * Get the internal subset of a document |
| 287 | * Returns a pointer to the DTD structure or NULL if not found |
| 288 | */ |
| 289 | |
| 290 | xmlDtdPtr |
| 291 | xmlGetIntSubset(xmlDocPtr doc) { |
| 292 | xmlNodePtr cur; |
| 293 | |
| 294 | if (doc == NULL) |
| 295 | return(NULL); |
| 296 | cur = doc->children; |
| 297 | while (cur != NULL) { |
| 298 | if (cur->type == XML_DTD_NODE) |
| 299 | return((xmlDtdPtr) cur); |
| 300 | cur = cur->next; |
| 301 | } |
| 302 | return((xmlDtdPtr) doc->intSubset); |
| 303 | } |
| 304 | |
| 305 | /** |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 306 | * xmlCreateIntSubset: |
| 307 | * @doc: the document pointer |
| 308 | * @name: the DTD name |
| 309 | * @ExternalID: the external ID |
| 310 | * @SystemID: the system ID |
| 311 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 312 | * Create the internal subset of a document |
| 313 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 314 | */ |
| 315 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 316 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 317 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 318 | xmlDtdPtr cur; |
| 319 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 320 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 321 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 322 | xmlGenericError(xmlGenericErrorContext, |
| 323 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 324 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 325 | doc->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 326 | #endif |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 327 | return(NULL); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Allocate a new DTD and fill the fields. |
| 332 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 333 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 334 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 335 | xmlGenericError(xmlGenericErrorContext, |
| 336 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +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 | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 341 | |
| 342 | if (name != NULL) |
| 343 | cur->name = xmlStrdup(name); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 344 | if (ExternalID != NULL) |
| 345 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 346 | if (SystemID != NULL) |
| 347 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 348 | if (doc != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 349 | doc->intSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 350 | cur->parent = doc; |
| 351 | cur->doc = doc; |
| 352 | if (doc->children == NULL) { |
| 353 | doc->children = (xmlNodePtr) cur; |
| 354 | doc->last = (xmlNodePtr) cur; |
| 355 | } else { |
| 356 | xmlNodePtr prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 357 | |
Daniel Veillard | b8f25c9 | 2000-08-19 19:52:36 +0000 | [diff] [blame] | 358 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 359 | prev = doc->children; |
| 360 | prev->prev = (xmlNodePtr) cur; |
| 361 | cur->next = prev; |
| 362 | doc->children = (xmlNodePtr) cur; |
| 363 | } else { |
| 364 | prev = doc->last; |
| 365 | prev->next = (xmlNodePtr) cur; |
| 366 | cur->prev = prev; |
| 367 | doc->last = (xmlNodePtr) cur; |
| 368 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 371 | return(cur); |
| 372 | } |
| 373 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 374 | /** |
| 375 | * xmlFreeDtd: |
| 376 | * @cur: the DTD structure to free up |
| 377 | * |
| 378 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 379 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 380 | void |
| 381 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 382 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 383 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 384 | xmlGenericError(xmlGenericErrorContext, |
| 385 | "xmlFreeDtd : DTD == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 386 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 387 | return; |
| 388 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 389 | if (cur->children != NULL) { |
| 390 | xmlNodePtr next, c = cur->children; |
| 391 | |
| 392 | /* |
| 393 | * Cleanup all the DTD comments they are not in the Dtd |
| 394 | * indexes. |
| 395 | */ |
| 396 | while (c != NULL) { |
| 397 | next = c->next; |
| 398 | if (c->type == XML_COMMENT_NODE) { |
| 399 | xmlUnlinkNode(c); |
| 400 | xmlFreeNode(c); |
| 401 | } |
| 402 | c = next; |
| 403 | } |
| 404 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 405 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 406 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 407 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 408 | /* TODO !!! */ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 409 | if (cur->notations != NULL) |
| 410 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 411 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 412 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 413 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 414 | if (cur->attributes != NULL) |
| 415 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 416 | if (cur->entities != NULL) |
| 417 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 418 | if (cur->pentities != NULL) |
| 419 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 420 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 421 | memset(cur, -1, sizeof(xmlDtd)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 422 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 425 | /** |
| 426 | * xmlNewDoc: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 427 | * @version: xmlChar string giving the version of XML "1.0" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 428 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 429 | * Returns a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 430 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 431 | xmlDocPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 432 | xmlNewDoc(const xmlChar *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 433 | xmlDocPtr cur; |
| 434 | |
Daniel Veillard | 7eda845 | 2000-10-14 23:38:43 +0000 | [diff] [blame] | 435 | if (version == NULL) |
| 436 | version = (const xmlChar *) "1.0"; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * Allocate a new document and fill the fields. |
| 440 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 441 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 442 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 443 | xmlGenericError(xmlGenericErrorContext, |
| 444 | "xmlNewDoc : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 445 | return(NULL); |
| 446 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 447 | memset(cur, 0, sizeof(xmlDoc)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 448 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 449 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 450 | cur->version = xmlStrdup(version); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 451 | cur->standalone = -1; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 452 | cur->compression = -1; /* not initialized */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 453 | cur->doc = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 454 | return(cur); |
| 455 | } |
| 456 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 457 | /** |
| 458 | * xmlFreeDoc: |
| 459 | * @cur: pointer to the document |
| 460 | * @: |
| 461 | * |
| 462 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 463 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 464 | void |
| 465 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 466 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 467 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 468 | xmlGenericError(xmlGenericErrorContext, |
| 469 | "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 470 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 471 | return; |
| 472 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 473 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 474 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 475 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 476 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 477 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 478 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 479 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 480 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 481 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 482 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 483 | memset(cur, -1, sizeof(xmlDoc)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 484 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 487 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 488 | * xmlStringLenGetNodeList: |
| 489 | * @doc: the document |
| 490 | * @value: the value of the text |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 491 | * @len: the length of the string value |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 492 | * |
| 493 | * Parse the value string and build the node list associated. Should |
| 494 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 495 | * Returns a pointer to the first child |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 496 | */ |
| 497 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 498 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 499 | xmlNodePtr ret = NULL, last = NULL; |
| 500 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 501 | xmlChar *val; |
| 502 | const xmlChar *cur = value; |
| 503 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 504 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 505 | |
| 506 | if (value == NULL) return(NULL); |
| 507 | |
| 508 | q = cur; |
| 509 | while ((*cur != 0) && (cur - value < len)) { |
| 510 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 511 | /* |
| 512 | * Save the current text. |
| 513 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 514 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 515 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 516 | xmlNodeAddContentLen(last, q, cur - q); |
| 517 | } else { |
| 518 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 519 | if (node == NULL) return(ret); |
| 520 | if (last == NULL) |
| 521 | last = ret = node; |
| 522 | else { |
| 523 | last->next = node; |
| 524 | node->prev = last; |
| 525 | last = node; |
| 526 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 529 | /* |
| 530 | * Read the entity string |
| 531 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 532 | cur++; |
| 533 | q = cur; |
| 534 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 535 | if ((*cur == 0) || (cur - value >= len)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 536 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 537 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 538 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 539 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 540 | return(ret); |
| 541 | } |
| 542 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 543 | /* |
| 544 | * Predefined entities don't generate nodes |
| 545 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 546 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 547 | ent = xmlGetDocEntity(doc, val); |
| 548 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 549 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 550 | if (last == NULL) { |
| 551 | node = xmlNewDocText(doc, ent->content); |
| 552 | last = ret = node; |
| 553 | } else |
| 554 | xmlNodeAddContent(last, ent->content); |
| 555 | |
| 556 | } else { |
| 557 | /* |
| 558 | * Create a new REFERENCE_REF node |
| 559 | */ |
| 560 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 561 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 562 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 563 | return(ret); |
| 564 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 565 | if (last == NULL) |
| 566 | last = ret = node; |
| 567 | else { |
| 568 | last->next = node; |
| 569 | node->prev = last; |
| 570 | last = node; |
| 571 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 572 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 573 | xmlFree(val); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 574 | } |
| 575 | cur++; |
| 576 | q = cur; |
| 577 | } else |
| 578 | cur++; |
| 579 | } |
| 580 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Handle the last piece of text. |
| 583 | */ |
| 584 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 585 | xmlNodeAddContentLen(last, q, cur - q); |
| 586 | } else { |
| 587 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 588 | if (node == NULL) return(ret); |
| 589 | if (last == NULL) |
| 590 | last = ret = node; |
| 591 | else { |
| 592 | last->next = node; |
| 593 | node->prev = last; |
| 594 | last = node; |
| 595 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | return(ret); |
| 599 | } |
| 600 | |
| 601 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 602 | * xmlStringGetNodeList: |
| 603 | * @doc: the document |
| 604 | * @value: the value of the attribute |
| 605 | * |
| 606 | * Parse the value string and build the node list associated. Should |
| 607 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 608 | * Returns a pointer to the first child |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 609 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 610 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 611 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 612 | xmlNodePtr ret = NULL, last = NULL; |
| 613 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 614 | xmlChar *val; |
| 615 | const xmlChar *cur = value; |
| 616 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 617 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 618 | |
| 619 | if (value == NULL) return(NULL); |
| 620 | |
| 621 | q = cur; |
| 622 | while (*cur != 0) { |
| 623 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 624 | /* |
| 625 | * Save the current text. |
| 626 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 627 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 628 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 629 | xmlNodeAddContentLen(last, q, cur - q); |
| 630 | } else { |
| 631 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 632 | if (node == NULL) return(ret); |
| 633 | if (last == NULL) |
| 634 | last = ret = node; |
| 635 | else { |
| 636 | last->next = node; |
| 637 | node->prev = last; |
| 638 | last = node; |
| 639 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 642 | /* |
| 643 | * Read the entity string |
| 644 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 645 | cur++; |
| 646 | q = cur; |
| 647 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 648 | if (*cur == 0) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 649 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 650 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 651 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 652 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 653 | return(ret); |
| 654 | } |
| 655 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 656 | /* |
| 657 | * Predefined entities don't generate nodes |
| 658 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 659 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 660 | ent = xmlGetDocEntity(doc, val); |
| 661 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 662 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 663 | if (last == NULL) { |
| 664 | node = xmlNewDocText(doc, ent->content); |
| 665 | last = ret = node; |
| 666 | } else |
| 667 | xmlNodeAddContent(last, ent->content); |
| 668 | |
| 669 | } else { |
| 670 | /* |
| 671 | * Create a new REFERENCE_REF node |
| 672 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 673 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 674 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 675 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 676 | return(ret); |
| 677 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 678 | if (last == NULL) |
| 679 | last = ret = node; |
| 680 | else { |
| 681 | last->next = node; |
| 682 | node->prev = last; |
| 683 | last = node; |
| 684 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 685 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 686 | xmlFree(val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 687 | } |
| 688 | cur++; |
| 689 | q = cur; |
| 690 | } else |
| 691 | cur++; |
| 692 | } |
| 693 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 694 | /* |
| 695 | * Handle the last piece of text. |
| 696 | */ |
| 697 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 698 | xmlNodeAddContentLen(last, q, cur - q); |
| 699 | } else { |
| 700 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 701 | if (node == NULL) return(ret); |
| 702 | if (last == NULL) |
| 703 | last = ret = node; |
| 704 | else { |
| 705 | last->next = node; |
| 706 | node->prev = last; |
| 707 | last = node; |
| 708 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | return(ret); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * xmlNodeListGetString: |
| 716 | * @doc: the document |
| 717 | * @list: a Node list |
| 718 | * @inLine: should we replace entity contents or show their external form |
| 719 | * |
| 720 | * Returns the string equivalent to the text contained in the Node list |
| 721 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 722 | * Returns a pointer to the string copy, the calller must free it. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 723 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 724 | xmlChar * |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 725 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 726 | xmlNodePtr node = list; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 727 | xmlChar *ret = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 728 | xmlEntityPtr ent; |
| 729 | |
| 730 | if (list == NULL) return(NULL); |
| 731 | |
| 732 | while (node != NULL) { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 733 | if ((node->type == XML_TEXT_NODE) || |
| 734 | (node->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 735 | if (inLine) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 736 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 737 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 738 | #else |
| 739 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 740 | #endif |
| 741 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 742 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 743 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 744 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 745 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 746 | #else |
| 747 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 748 | xmlBufferContent(node->content)); |
| 749 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 750 | if (buffer != NULL) { |
| 751 | ret = xmlStrcat(ret, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 752 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 755 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 756 | if (inLine) { |
| 757 | ent = xmlGetDocEntity(doc, node->name); |
| 758 | if (ent != NULL) |
| 759 | ret = xmlStrcat(ret, ent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 760 | else { |
| 761 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 762 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 763 | #else |
| 764 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 765 | #endif |
| 766 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 767 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 768 | xmlChar buf[2]; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 769 | buf[0] = '&'; buf[1] = 0; |
| 770 | ret = xmlStrncat(ret, buf, 1); |
| 771 | ret = xmlStrcat(ret, node->name); |
| 772 | buf[0] = ';'; buf[1] = 0; |
| 773 | ret = xmlStrncat(ret, buf, 1); |
| 774 | } |
| 775 | } |
| 776 | #if 0 |
| 777 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 778 | xmlGenericError(xmlGenericErrorContext, |
| 779 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 780 | node->type); |
| 781 | } |
| 782 | #endif |
| 783 | node = node->next; |
| 784 | } |
| 785 | return(ret); |
| 786 | } |
| 787 | |
| 788 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 789 | * xmlNodeListGetRawString: |
| 790 | * @doc: the document |
| 791 | * @list: a Node list |
| 792 | * @inLine: should we replace entity contents or show their external form |
| 793 | * |
| 794 | * Returns the string equivalent to the text contained in the Node list |
| 795 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 796 | * this function doesn't do any character encoding handling. |
| 797 | * |
| 798 | * Returns a pointer to the string copy, the calller must free it. |
| 799 | */ |
| 800 | xmlChar * |
| 801 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 802 | xmlNodePtr node = list; |
| 803 | xmlChar *ret = NULL; |
| 804 | xmlEntityPtr ent; |
| 805 | |
| 806 | if (list == NULL) return(NULL); |
| 807 | |
| 808 | while (node != NULL) { |
| 809 | if (node->type == XML_TEXT_NODE) { |
| 810 | if (inLine) { |
| 811 | #ifndef XML_USE_BUFFER_CONTENT |
| 812 | ret = xmlStrcat(ret, node->content); |
| 813 | #else |
| 814 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 815 | #endif |
| 816 | } else { |
| 817 | xmlChar *buffer; |
| 818 | |
| 819 | #ifndef XML_USE_BUFFER_CONTENT |
| 820 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 821 | #else |
| 822 | buffer = xmlEncodeSpecialChars(doc, |
| 823 | xmlBufferContent(node->content)); |
| 824 | #endif |
| 825 | if (buffer != NULL) { |
| 826 | ret = xmlStrcat(ret, buffer); |
| 827 | xmlFree(buffer); |
| 828 | } |
| 829 | } |
| 830 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 831 | if (inLine) { |
| 832 | ent = xmlGetDocEntity(doc, node->name); |
| 833 | if (ent != NULL) |
| 834 | ret = xmlStrcat(ret, ent->content); |
| 835 | else { |
| 836 | #ifndef XML_USE_BUFFER_CONTENT |
| 837 | ret = xmlStrcat(ret, node->content); |
| 838 | #else |
| 839 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 840 | #endif |
| 841 | } |
| 842 | } else { |
| 843 | xmlChar buf[2]; |
| 844 | buf[0] = '&'; buf[1] = 0; |
| 845 | ret = xmlStrncat(ret, buf, 1); |
| 846 | ret = xmlStrcat(ret, node->name); |
| 847 | buf[0] = ';'; buf[1] = 0; |
| 848 | ret = xmlStrncat(ret, buf, 1); |
| 849 | } |
| 850 | } |
| 851 | #if 0 |
| 852 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 853 | xmlGenericError(xmlGenericErrorContext, |
| 854 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 855 | node->type); |
| 856 | } |
| 857 | #endif |
| 858 | node = node->next; |
| 859 | } |
| 860 | return(ret); |
| 861 | } |
| 862 | |
| 863 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 864 | * xmlNewProp: |
| 865 | * @node: the holding node |
| 866 | * @name: the name of the attribute |
| 867 | * @value: the value of the attribute |
| 868 | * |
| 869 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 870 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 871 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 872 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 873 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 874 | xmlAttrPtr cur; |
| 875 | |
| 876 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 877 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 878 | xmlGenericError(xmlGenericErrorContext, |
| 879 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 880 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 881 | return(NULL); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Allocate a new property and fill the fields. |
| 886 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 887 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 888 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 889 | xmlGenericError(xmlGenericErrorContext, |
| 890 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 891 | return(NULL); |
| 892 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 893 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 894 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 895 | |
| 896 | cur->parent = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 897 | cur->name = xmlStrdup(name); |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 898 | if (value != NULL) { |
| 899 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 900 | xmlNodePtr tmp; |
| 901 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 902 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 903 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 904 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 905 | tmp = cur->children; |
| 906 | while (tmp != NULL) { |
| 907 | tmp->parent = (xmlNodePtr) cur; |
| 908 | if (tmp->next == NULL) |
| 909 | cur->last = tmp; |
| 910 | tmp = tmp->next; |
| 911 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 912 | xmlFree(buffer); |
| 913 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * Add it at the end to preserve parsing order ... |
| 917 | */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 918 | if (node != NULL) { |
| 919 | if (node->properties == NULL) { |
| 920 | node->properties = cur; |
| 921 | } else { |
| 922 | xmlAttrPtr prev = node->properties; |
| 923 | |
| 924 | while (prev->next != NULL) prev = prev->next; |
| 925 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 926 | cur->prev = prev; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | return(cur); |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * xmlNewNsProp: |
| 934 | * @node: the holding node |
| 935 | * @ns: the namespace |
| 936 | * @name: the name of the attribute |
| 937 | * @value: the value of the attribute |
| 938 | * |
| 939 | * Create a new property tagged with a namespace and carried by a node. |
| 940 | * Returns a pointer to the attribute |
| 941 | */ |
| 942 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 943 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 944 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 945 | xmlAttrPtr cur; |
| 946 | |
| 947 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 948 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 949 | xmlGenericError(xmlGenericErrorContext, |
| 950 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 951 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 952 | return(NULL); |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Allocate a new property and fill the fields. |
| 957 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 958 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 959 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 960 | xmlGenericError(xmlGenericErrorContext, |
| 961 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 962 | return(NULL); |
| 963 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 964 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 965 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 966 | |
| 967 | cur->parent = node; |
| 968 | if (node != NULL) |
| 969 | cur->doc = node->doc; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 970 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 971 | cur->name = xmlStrdup(name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 972 | if (value != NULL) { |
| 973 | xmlChar *buffer; |
| 974 | xmlNodePtr tmp; |
| 975 | |
| 976 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 977 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 978 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 979 | tmp = cur->children; |
| 980 | while (tmp != NULL) { |
| 981 | tmp->parent = (xmlNodePtr) cur; |
| 982 | if (tmp->next == NULL) |
| 983 | cur->last = tmp; |
| 984 | tmp = tmp->next; |
| 985 | } |
| 986 | xmlFree(buffer); |
| 987 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 988 | |
| 989 | /* |
| 990 | * Add it at the end to preserve parsing order ... |
| 991 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 992 | if (node != NULL) { |
| 993 | if (node->properties == NULL) { |
| 994 | node->properties = cur; |
| 995 | } else { |
| 996 | xmlAttrPtr prev = node->properties; |
| 997 | |
| 998 | while (prev->next != NULL) prev = prev->next; |
| 999 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1000 | cur->prev = prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | return(cur); |
| 1004 | } |
| 1005 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1006 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1007 | * xmlNewDocProp: |
| 1008 | * @doc: the document |
| 1009 | * @name: the name of the attribute |
| 1010 | * @value: the value of the attribute |
| 1011 | * |
| 1012 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1013 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1014 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1015 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1016 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1017 | xmlAttrPtr cur; |
| 1018 | |
| 1019 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1020 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1021 | xmlGenericError(xmlGenericErrorContext, |
| 1022 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1023 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1024 | return(NULL); |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Allocate a new property and fill the fields. |
| 1029 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1030 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1031 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1032 | xmlGenericError(xmlGenericErrorContext, |
| 1033 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1034 | return(NULL); |
| 1035 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1036 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1037 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1038 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1039 | cur->name = xmlStrdup(name); |
| 1040 | cur->doc = doc; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1041 | if (value != NULL) { |
| 1042 | xmlNodePtr tmp; |
| 1043 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1044 | cur->children = xmlStringGetNodeList(doc, value); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1045 | cur->last = NULL; |
| 1046 | |
| 1047 | tmp = cur->children; |
| 1048 | while (tmp != NULL) { |
| 1049 | tmp->parent = (xmlNodePtr) cur; |
| 1050 | if (tmp->next == NULL) |
| 1051 | cur->last = tmp; |
| 1052 | tmp = tmp->next; |
| 1053 | } |
| 1054 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1055 | return(cur); |
| 1056 | } |
| 1057 | |
| 1058 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1059 | * xmlFreePropList: |
| 1060 | * @cur: the first property in the list |
| 1061 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1062 | * 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] | 1063 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1064 | void |
| 1065 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1066 | xmlAttrPtr next; |
| 1067 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1068 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1069 | xmlGenericError(xmlGenericErrorContext, |
| 1070 | "xmlFreePropList : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1071 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | while (cur != NULL) { |
| 1075 | next = cur->next; |
| 1076 | xmlFreeProp(cur); |
| 1077 | cur = next; |
| 1078 | } |
| 1079 | } |
| 1080 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1081 | /** |
| 1082 | * xmlFreeProp: |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1083 | * @cur: an attribute |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1084 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1085 | * Free one attribute, all the content is freed too |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1086 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1087 | void |
| 1088 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1089 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1090 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1091 | xmlGenericError(xmlGenericErrorContext, |
| 1092 | "xmlFreeProp : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1093 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1094 | return; |
| 1095 | } |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 1096 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1097 | if ((cur->parent != NULL) && |
| 1098 | (xmlIsID(cur->parent->doc, cur->parent, cur))) |
| 1099 | xmlRemoveID(cur->parent->doc, cur); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1100 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1101 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1102 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1103 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1106 | /** |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1107 | * xmlRemoveProp: |
| 1108 | * @cur: an attribute |
| 1109 | * |
| 1110 | * Unlink and free one attribute, all the content is freed too |
| 1111 | * Note this doesn't work for namespace definition attributes |
| 1112 | * |
| 1113 | * Returns 0 if success and -1 in case of error. |
| 1114 | */ |
| 1115 | int |
| 1116 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1117 | xmlAttrPtr tmp; |
| 1118 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1119 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1120 | xmlGenericError(xmlGenericErrorContext, |
| 1121 | "xmlRemoveProp : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1122 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1123 | return(-1); |
| 1124 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1125 | if (cur->parent == NULL) { |
| 1126 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1127 | xmlGenericError(xmlGenericErrorContext, |
| 1128 | "xmlRemoveProp : cur->parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1129 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1130 | return(-1); |
| 1131 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1132 | tmp = cur->parent->properties; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1133 | if (tmp == cur) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1134 | cur->parent->properties = cur->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1135 | xmlFreeProp(cur); |
| 1136 | return(0); |
| 1137 | } |
| 1138 | while (tmp != NULL) { |
| 1139 | if (tmp->next == cur) { |
| 1140 | tmp->next = cur->next; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1141 | if (tmp->next != NULL) |
| 1142 | tmp->next->prev = tmp; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1143 | xmlFreeProp(cur); |
| 1144 | return(0); |
| 1145 | } |
| 1146 | tmp = tmp->next; |
| 1147 | } |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1148 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1149 | xmlGenericError(xmlGenericErrorContext, |
| 1150 | "xmlRemoveProp : attribute not owned by its node\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1151 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1152 | return(-1); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1156 | * xmlNewPI: |
| 1157 | * @name: the processing instruction name |
| 1158 | * @content: the PI content |
| 1159 | * |
| 1160 | * Creation of a processing instruction element. |
| 1161 | * Returns a pointer to the new node object. |
| 1162 | */ |
| 1163 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1164 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1165 | xmlNodePtr cur; |
| 1166 | |
| 1167 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1168 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1169 | xmlGenericError(xmlGenericErrorContext, |
| 1170 | "xmlNewPI : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1171 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1172 | return(NULL); |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Allocate a new node and fill the fields. |
| 1177 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1178 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1179 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1180 | xmlGenericError(xmlGenericErrorContext, |
| 1181 | "xmlNewPI : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1182 | return(NULL); |
| 1183 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1184 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1185 | cur->type = XML_PI_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1186 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1187 | cur->name = xmlStrdup(name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1188 | if (content != NULL) { |
| 1189 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1190 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1191 | #else |
| 1192 | cur->content = xmlBufferCreateSize(0); |
| 1193 | xmlBufferSetAllocationScheme(cur->content, |
| 1194 | xmlGetBufferAllocationScheme()); |
| 1195 | xmlBufferAdd(cur->content, content, -1); |
| 1196 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1197 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1198 | return(cur); |
| 1199 | } |
| 1200 | |
| 1201 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1202 | * xmlNewNode: |
| 1203 | * @ns: namespace if any |
| 1204 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1205 | * |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 1206 | * Creation of a new node element. @ns is optionnal (NULL). |
| 1207 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1208 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1209 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1210 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1211 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1212 | xmlNodePtr cur; |
| 1213 | |
| 1214 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1215 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1216 | xmlGenericError(xmlGenericErrorContext, |
| 1217 | "xmlNewNode : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1218 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1219 | return(NULL); |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * Allocate a new node and fill the fields. |
| 1224 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1225 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1226 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1227 | xmlGenericError(xmlGenericErrorContext, |
| 1228 | "xmlNewNode : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1229 | return(NULL); |
| 1230 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1231 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1232 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1233 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1234 | cur->name = xmlStrdup(name); |
| 1235 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1236 | return(cur); |
| 1237 | } |
| 1238 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1239 | /** |
| 1240 | * xmlNewDocNode: |
| 1241 | * @doc: the document |
| 1242 | * @ns: namespace if any |
| 1243 | * @name: the node name |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1244 | * @content: the XML text content if any |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1245 | * |
| 1246 | * Creation of a new node element within a document. @ns and @content |
| 1247 | * are optionnal (NULL). |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1248 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1249 | * references, but XML special chars need to be escaped first by using |
| 1250 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1251 | * need entities support. |
| 1252 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1253 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1254 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1255 | xmlNodePtr |
| 1256 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1257 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1258 | xmlNodePtr cur; |
| 1259 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1260 | cur = xmlNewNode(ns, name); |
| 1261 | if (cur != NULL) { |
| 1262 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1263 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1264 | cur->children = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1265 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1266 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1267 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1268 | return(cur); |
| 1269 | } |
| 1270 | |
| 1271 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1272 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1273 | * xmlNewDocRawNode: |
| 1274 | * @doc: the document |
| 1275 | * @ns: namespace if any |
| 1276 | * @name: the node name |
| 1277 | * @content: the text content if any |
| 1278 | * |
| 1279 | * Creation of a new node element within a document. @ns and @content |
| 1280 | * are optionnal (NULL). |
| 1281 | * |
| 1282 | * Returns a pointer to the new node object. |
| 1283 | */ |
| 1284 | xmlNodePtr |
| 1285 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1286 | const xmlChar *name, const xmlChar *content) { |
| 1287 | xmlNodePtr cur; |
| 1288 | |
| 1289 | cur = xmlNewNode(ns, name); |
| 1290 | if (cur != NULL) { |
| 1291 | cur->doc = doc; |
| 1292 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1293 | cur->children = xmlNewDocText(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1294 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | return(cur); |
| 1298 | } |
| 1299 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1300 | /** |
| 1301 | * xmlNewDocFragment: |
| 1302 | * @doc: the document owning the fragment |
| 1303 | * |
| 1304 | * Creation of a new Fragment node. |
| 1305 | * Returns a pointer to the new node object. |
| 1306 | */ |
| 1307 | xmlNodePtr |
| 1308 | xmlNewDocFragment(xmlDocPtr doc) { |
| 1309 | xmlNodePtr cur; |
| 1310 | |
| 1311 | /* |
| 1312 | * Allocate a new DocumentFragment node and fill the fields. |
| 1313 | */ |
| 1314 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1315 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1316 | xmlGenericError(xmlGenericErrorContext, |
| 1317 | "xmlNewDocFragment : malloc failed\n"); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1318 | return(NULL); |
| 1319 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1320 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1321 | cur->type = XML_DOCUMENT_FRAG_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1322 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1323 | cur->doc = doc; |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1324 | return(cur); |
| 1325 | } |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1326 | |
| 1327 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1328 | * xmlNewText: |
| 1329 | * @content: the text content |
| 1330 | * |
| 1331 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1332 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1333 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1334 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1335 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1336 | xmlNodePtr cur; |
| 1337 | |
| 1338 | /* |
| 1339 | * Allocate a new node and fill the fields. |
| 1340 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1341 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1342 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1343 | xmlGenericError(xmlGenericErrorContext, |
| 1344 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1345 | return(NULL); |
| 1346 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1347 | memset(cur, 0, sizeof(xmlNode)); |
| 1348 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1349 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1350 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1351 | if (content != NULL) { |
| 1352 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1353 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1354 | #else |
| 1355 | cur->content = xmlBufferCreateSize(0); |
| 1356 | xmlBufferSetAllocationScheme(cur->content, |
| 1357 | xmlGetBufferAllocationScheme()); |
| 1358 | xmlBufferAdd(cur->content, content, -1); |
| 1359 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1360 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1361 | return(cur); |
| 1362 | } |
| 1363 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1364 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1365 | * xmlNewTextChild: |
| 1366 | * @parent: the parent node |
| 1367 | * @ns: a namespace if any |
| 1368 | * @name: the name of the child |
| 1369 | * @content: the text content of the child if any. |
| 1370 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1371 | * 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] | 1372 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1373 | * a child TEXT node will be created containing the string content. |
| 1374 | * |
| 1375 | * Returns a pointer to the new node object. |
| 1376 | */ |
| 1377 | xmlNodePtr |
| 1378 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1379 | const xmlChar *name, const xmlChar *content) { |
| 1380 | xmlNodePtr cur, prev; |
| 1381 | |
| 1382 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1383 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1384 | xmlGenericError(xmlGenericErrorContext, |
| 1385 | "xmlNewTextChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1386 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1387 | return(NULL); |
| 1388 | } |
| 1389 | |
| 1390 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1391 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1392 | xmlGenericError(xmlGenericErrorContext, |
| 1393 | "xmlNewTextChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1394 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1395 | return(NULL); |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * Allocate a new node |
| 1400 | */ |
| 1401 | if (ns == NULL) |
| 1402 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1403 | else |
| 1404 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1405 | if (cur == NULL) return(NULL); |
| 1406 | |
| 1407 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1408 | * add the new element at the end of the children list. |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1409 | */ |
| 1410 | cur->type = XML_ELEMENT_NODE; |
| 1411 | cur->parent = parent; |
| 1412 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1413 | if (parent->children == NULL) { |
| 1414 | parent->children = cur; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1415 | parent->last = cur; |
| 1416 | } else { |
| 1417 | prev = parent->last; |
| 1418 | prev->next = cur; |
| 1419 | cur->prev = prev; |
| 1420 | parent->last = cur; |
| 1421 | } |
| 1422 | |
| 1423 | return(cur); |
| 1424 | } |
| 1425 | |
| 1426 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1427 | * xmlNewCharRef: |
| 1428 | * @doc: the document |
| 1429 | * @name: the char ref string, starting with # or "&# ... ;" |
| 1430 | * |
| 1431 | * Creation of a new character reference node. |
| 1432 | * Returns a pointer to the new node object. |
| 1433 | */ |
| 1434 | xmlNodePtr |
| 1435 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 1436 | xmlNodePtr cur; |
| 1437 | |
| 1438 | /* |
| 1439 | * Allocate a new node and fill the fields. |
| 1440 | */ |
| 1441 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1442 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1443 | xmlGenericError(xmlGenericErrorContext, |
| 1444 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1445 | return(NULL); |
| 1446 | } |
| 1447 | memset(cur, 0, sizeof(xmlNode)); |
| 1448 | cur->type = XML_ENTITY_REF_NODE; |
| 1449 | |
| 1450 | cur->doc = doc; |
| 1451 | if (name[0] == '&') { |
| 1452 | int len; |
| 1453 | name++; |
| 1454 | len = xmlStrlen(name); |
| 1455 | if (name[len - 1] == ';') |
| 1456 | cur->name = xmlStrndup(name, len - 1); |
| 1457 | else |
| 1458 | cur->name = xmlStrndup(name, len); |
| 1459 | } else |
| 1460 | cur->name = xmlStrdup(name); |
| 1461 | return(cur); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1465 | * xmlNewReference: |
| 1466 | * @doc: the document |
| 1467 | * @name: the reference name, or the reference string with & and ; |
| 1468 | * |
| 1469 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1470 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1471 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1472 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1473 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1474 | xmlNodePtr cur; |
| 1475 | xmlEntityPtr ent; |
| 1476 | |
| 1477 | /* |
| 1478 | * Allocate a new node and fill the fields. |
| 1479 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1480 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1481 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1482 | xmlGenericError(xmlGenericErrorContext, |
| 1483 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1484 | return(NULL); |
| 1485 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1486 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1487 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1488 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1489 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1490 | if (name[0] == '&') { |
| 1491 | int len; |
| 1492 | name++; |
| 1493 | len = xmlStrlen(name); |
| 1494 | if (name[len - 1] == ';') |
| 1495 | cur->name = xmlStrndup(name, len - 1); |
| 1496 | else |
| 1497 | cur->name = xmlStrndup(name, len); |
| 1498 | } else |
| 1499 | cur->name = xmlStrdup(name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1500 | |
| 1501 | ent = xmlGetDocEntity(doc, cur->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1502 | if (ent != NULL) { |
| 1503 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1504 | cur->content = ent->content; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1505 | #else |
| 1506 | /* |
| 1507 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1508 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1509 | * later |
| 1510 | */ |
| 1511 | cur->content = xmlBufferCreateSize(0); |
| 1512 | xmlBufferSetAllocationScheme(cur->content, |
| 1513 | xmlGetBufferAllocationScheme()); |
| 1514 | if (ent->content != NULL) |
| 1515 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1516 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1517 | /* |
| 1518 | * The parent pointer in entity is a Dtd pointer and thus is NOT |
| 1519 | * updated. Not sure if this is 100% correct. |
| 1520 | * -George |
| 1521 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1522 | cur->children = (xmlNodePtr) ent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1523 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1524 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1525 | return(cur); |
| 1526 | } |
| 1527 | |
| 1528 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1529 | * xmlNewDocText: |
| 1530 | * @doc: the document |
| 1531 | * @content: the text content |
| 1532 | * |
| 1533 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1534 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1535 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1536 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1537 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1538 | xmlNodePtr cur; |
| 1539 | |
| 1540 | cur = xmlNewText(content); |
| 1541 | if (cur != NULL) cur->doc = doc; |
| 1542 | return(cur); |
| 1543 | } |
| 1544 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1545 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1546 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1547 | * @content: the text content |
| 1548 | * @len: the text len. |
| 1549 | * |
| 1550 | * 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] | 1551 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1552 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1553 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1554 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1555 | xmlNodePtr cur; |
| 1556 | |
| 1557 | /* |
| 1558 | * Allocate a new node and fill the fields. |
| 1559 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1560 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1561 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1562 | xmlGenericError(xmlGenericErrorContext, |
| 1563 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1564 | return(NULL); |
| 1565 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1566 | memset(cur, 0, sizeof(xmlNode)); |
| 1567 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1568 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1569 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1570 | if (content != NULL) { |
| 1571 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1572 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1573 | #else |
| 1574 | cur->content = xmlBufferCreateSize(len); |
| 1575 | xmlBufferSetAllocationScheme(cur->content, |
| 1576 | xmlGetBufferAllocationScheme()); |
| 1577 | xmlBufferAdd(cur->content, content, len); |
| 1578 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1579 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1580 | return(cur); |
| 1581 | } |
| 1582 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1583 | /** |
| 1584 | * xmlNewDocTextLen: |
| 1585 | * @doc: the document |
| 1586 | * @content: the text content |
| 1587 | * @len: the text len. |
| 1588 | * |
| 1589 | * Creation of a new text node with an extra content lenght parameter. The |
| 1590 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1591 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1592 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1593 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1594 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1595 | xmlNodePtr cur; |
| 1596 | |
| 1597 | cur = xmlNewTextLen(content, len); |
| 1598 | if (cur != NULL) cur->doc = doc; |
| 1599 | return(cur); |
| 1600 | } |
| 1601 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1602 | /** |
| 1603 | * xmlNewComment: |
| 1604 | * @content: the comment content |
| 1605 | * |
| 1606 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1607 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1608 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1609 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1610 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1611 | xmlNodePtr cur; |
| 1612 | |
| 1613 | /* |
| 1614 | * Allocate a new node and fill the fields. |
| 1615 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1616 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1617 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1618 | xmlGenericError(xmlGenericErrorContext, |
| 1619 | "xmlNewComment : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1620 | return(NULL); |
| 1621 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1622 | memset(cur, 0, sizeof(xmlNode)); |
| 1623 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1624 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1625 | cur->name = xmlStrdup(xmlStringComment); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1626 | if (content != NULL) { |
| 1627 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1628 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1629 | #else |
| 1630 | cur->content = xmlBufferCreateSize(0); |
| 1631 | xmlBufferSetAllocationScheme(cur->content, |
| 1632 | xmlGetBufferAllocationScheme()); |
| 1633 | xmlBufferAdd(cur->content, content, -1); |
| 1634 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1635 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1636 | return(cur); |
| 1637 | } |
| 1638 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1639 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1640 | * xmlNewCDataBlock: |
| 1641 | * @doc: the document |
| 1642 | * @content: the CData block content content |
| 1643 | * @len: the length of the block |
| 1644 | * |
| 1645 | * Creation of a new node containing a CData block. |
| 1646 | * Returns a pointer to the new node object. |
| 1647 | */ |
| 1648 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1649 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1650 | xmlNodePtr cur; |
| 1651 | |
| 1652 | /* |
| 1653 | * Allocate a new node and fill the fields. |
| 1654 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1655 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1656 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1657 | xmlGenericError(xmlGenericErrorContext, |
| 1658 | "xmlNewCDataBlock : malloc failed\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1659 | return(NULL); |
| 1660 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1661 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1662 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1663 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1664 | if (content != NULL) { |
| 1665 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1666 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1667 | #else |
| 1668 | cur->content = xmlBufferCreateSize(len); |
| 1669 | xmlBufferSetAllocationScheme(cur->content, |
| 1670 | xmlGetBufferAllocationScheme()); |
| 1671 | xmlBufferAdd(cur->content, content, len); |
| 1672 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1673 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1674 | return(cur); |
| 1675 | } |
| 1676 | |
| 1677 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1678 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1679 | * @doc: the document |
| 1680 | * @content: the comment content |
| 1681 | * |
| 1682 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1683 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1684 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1685 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1686 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1687 | xmlNodePtr cur; |
| 1688 | |
| 1689 | cur = xmlNewComment(content); |
| 1690 | if (cur != NULL) cur->doc = doc; |
| 1691 | return(cur); |
| 1692 | } |
| 1693 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1694 | /** |
| 1695 | * xmlSetTreeDoc: |
| 1696 | * @tree: the top element |
| 1697 | * @doc: the document |
| 1698 | * |
| 1699 | * update all nodes under the tree to point to the right document |
| 1700 | */ |
| 1701 | void |
| 1702 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
| 1703 | if (tree == NULL) |
| 1704 | return; |
| 1705 | if (tree->type == XML_ENTITY_DECL) |
| 1706 | return; |
| 1707 | if (tree->doc != doc) { |
| 1708 | if (tree->children != NULL) |
| 1709 | xmlSetListDoc(tree->children, doc); |
| 1710 | tree->doc = doc; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | /** |
| 1715 | * xmlSetListDoc: |
| 1716 | * @tree: the first element |
| 1717 | * @doc: the document |
| 1718 | * |
| 1719 | * update all nodes in the list to point to the right document |
| 1720 | */ |
| 1721 | void |
| 1722 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 1723 | xmlNodePtr cur; |
| 1724 | |
| 1725 | if (list == NULL) |
| 1726 | return; |
| 1727 | cur = list; |
| 1728 | while (cur != NULL) { |
| 1729 | if (cur->doc != doc) |
| 1730 | xmlSetTreeDoc(cur, doc); |
| 1731 | cur = cur->next; |
| 1732 | } |
| 1733 | } |
| 1734 | |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1735 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1736 | /** |
| 1737 | * xmlNewChild: |
| 1738 | * @parent: the parent node |
| 1739 | * @ns: a namespace if any |
| 1740 | * @name: the name of the child |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1741 | * @content: the XML content of the child if any. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1742 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1743 | * 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] | 1744 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1745 | * 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] | 1746 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1747 | * references, but XML special chars need to be escaped first by using |
| 1748 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1749 | * support is not needed. |
| 1750 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1751 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1752 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1753 | xmlNodePtr |
| 1754 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1755 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1756 | xmlNodePtr cur, prev; |
| 1757 | |
| 1758 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1759 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1760 | xmlGenericError(xmlGenericErrorContext, |
| 1761 | "xmlNewChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1762 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1763 | return(NULL); |
| 1764 | } |
| 1765 | |
| 1766 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1767 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1768 | xmlGenericError(xmlGenericErrorContext, |
| 1769 | "xmlNewChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1770 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1771 | return(NULL); |
| 1772 | } |
| 1773 | |
| 1774 | /* |
| 1775 | * Allocate a new node |
| 1776 | */ |
| 1777 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1778 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1779 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1780 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1781 | if (cur == NULL) return(NULL); |
| 1782 | |
| 1783 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1784 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1785 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1786 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1787 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1788 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1789 | if (parent->children == NULL) { |
| 1790 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1791 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1792 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1793 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1794 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1795 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1796 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | return(cur); |
| 1800 | } |
| 1801 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1802 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1803 | * xmlAddNextSibling: |
| 1804 | * @cur: the child node |
| 1805 | * @elem: the new node |
| 1806 | * |
| 1807 | * Add a new element @elem as the next siblings of @cur |
| 1808 | * If the new element was already inserted in a document it is |
| 1809 | * first unlinked from its existing context. |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1810 | * As a result of text merging @elem may be freed. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1811 | * |
| 1812 | * Returns the new element or NULL in case of error. |
| 1813 | */ |
| 1814 | xmlNodePtr |
| 1815 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1816 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1817 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1818 | xmlGenericError(xmlGenericErrorContext, |
| 1819 | "xmlAddNextSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1820 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1821 | return(NULL); |
| 1822 | } |
| 1823 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1824 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1825 | xmlGenericError(xmlGenericErrorContext, |
| 1826 | "xmlAddNextSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1827 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1828 | return(NULL); |
| 1829 | } |
| 1830 | |
| 1831 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1832 | |
| 1833 | if (elem->type == XML_TEXT_NODE) { |
| 1834 | if (cur->type == XML_TEXT_NODE) { |
| 1835 | #ifndef XML_USE_BUFFER_CONTENT |
| 1836 | xmlNodeAddContent(cur, elem->content); |
| 1837 | #else |
| 1838 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1839 | #endif |
| 1840 | xmlFreeNode(elem); |
| 1841 | return(cur); |
| 1842 | } |
| 1843 | if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) { |
| 1844 | #ifndef XML_USE_BUFFER_CONTENT |
| 1845 | xmlChar *tmp; |
| 1846 | |
| 1847 | tmp = xmlStrdup(elem->content); |
| 1848 | tmp = xmlStrcat(tmp, cur->next->content); |
| 1849 | xmlNodeSetContent(cur->next, tmp); |
| 1850 | xmlFree(tmp); |
| 1851 | #else |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1852 | xmlBufferAddHead(cur->next->content, |
| 1853 | xmlBufferContent(elem->content), |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1854 | xmlBufferLength(elem->content)); |
| 1855 | #endif |
| 1856 | xmlFreeNode(elem); |
| 1857 | return(cur->next); |
| 1858 | } |
| 1859 | } |
| 1860 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1861 | if (elem->doc != cur->doc) { |
| 1862 | xmlSetTreeDoc(elem, cur->doc); |
| 1863 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1864 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1865 | elem->prev = cur; |
| 1866 | elem->next = cur->next; |
| 1867 | cur->next = elem; |
| 1868 | if (elem->next != NULL) |
| 1869 | elem->next->prev = elem; |
| 1870 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1871 | elem->parent->last = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1872 | return(elem); |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * xmlAddPrevSibling: |
| 1877 | * @cur: the child node |
| 1878 | * @elem: the new node |
| 1879 | * |
| 1880 | * Add a new element @elem as the previous siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1881 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1882 | * If the new element was already inserted in a document it is |
| 1883 | * first unlinked from its existing context. |
| 1884 | * |
| 1885 | * Returns the new element or NULL in case of error. |
| 1886 | */ |
| 1887 | xmlNodePtr |
| 1888 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1889 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1890 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1891 | xmlGenericError(xmlGenericErrorContext, |
| 1892 | "xmlAddPrevSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1893 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1894 | return(NULL); |
| 1895 | } |
| 1896 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1897 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1898 | xmlGenericError(xmlGenericErrorContext, |
| 1899 | "xmlAddPrevSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1900 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1901 | return(NULL); |
| 1902 | } |
| 1903 | |
| 1904 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1905 | |
| 1906 | if (elem->type == XML_TEXT_NODE) { |
| 1907 | if (cur->type == XML_TEXT_NODE) { |
| 1908 | #ifndef XML_USE_BUFFER_CONTENT |
| 1909 | xmlChar *tmp; |
| 1910 | |
| 1911 | tmp = xmlStrdup(elem->content); |
| 1912 | tmp = xmlStrcat(tmp, cur->content); |
| 1913 | xmlNodeSetContent(cur, tmp); |
| 1914 | xmlFree(tmp); |
| 1915 | #else |
| 1916 | xmlBufferAddHead(cur->content, xmlBufferContent(elem->content), |
| 1917 | xmlBufferLength(elem->content)); |
| 1918 | #endif |
| 1919 | xmlFreeNode(elem); |
| 1920 | return(cur); |
| 1921 | } |
| 1922 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) { |
| 1923 | #ifndef XML_USE_BUFFER_CONTENT |
| 1924 | xmlNodeAddContent(cur->prev, elem->content); |
| 1925 | #else |
| 1926 | xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content)); |
| 1927 | #endif |
| 1928 | xmlFreeNode(elem); |
| 1929 | return(cur->prev); |
| 1930 | } |
| 1931 | } |
| 1932 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1933 | if (elem->doc != cur->doc) { |
| 1934 | xmlSetTreeDoc(elem, cur->doc); |
| 1935 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1936 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1937 | elem->next = cur; |
| 1938 | elem->prev = cur->prev; |
| 1939 | cur->prev = elem; |
| 1940 | if (elem->prev != NULL) |
| 1941 | elem->prev->next = elem; |
| 1942 | if ((elem->parent != NULL) && (elem->parent->children == cur)) |
| 1943 | elem->parent->children = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1944 | return(elem); |
| 1945 | } |
| 1946 | |
| 1947 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1948 | * xmlAddSibling: |
| 1949 | * @cur: the child node |
| 1950 | * @elem: the new node |
| 1951 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1952 | * Add a new element @elem to the list of siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1953 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1954 | * If the new element was already inserted in a document it is |
| 1955 | * first unlinked from its existing context. |
| 1956 | * |
| 1957 | * Returns the new element or NULL in case of error. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1958 | */ |
| 1959 | xmlNodePtr |
| 1960 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1961 | xmlNodePtr parent; |
| 1962 | |
| 1963 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1964 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1965 | xmlGenericError(xmlGenericErrorContext, |
| 1966 | "xmlAddSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1967 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1968 | return(NULL); |
| 1969 | } |
| 1970 | |
| 1971 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1972 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1973 | xmlGenericError(xmlGenericErrorContext, |
| 1974 | "xmlAddSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1975 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1976 | return(NULL); |
| 1977 | } |
| 1978 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1979 | /* |
| 1980 | * Constant time is we can rely on the ->parent->last to find |
| 1981 | * the last sibling. |
| 1982 | */ |
| 1983 | if ((cur->parent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1984 | (cur->parent->children != NULL) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1985 | (cur->parent->last != NULL) && |
| 1986 | (cur->parent->last->next == NULL)) { |
| 1987 | cur = cur->parent->last; |
| 1988 | } else { |
| 1989 | while (cur->next != NULL) cur = cur->next; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1992 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1993 | |
| 1994 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
| 1995 | #ifndef XML_USE_BUFFER_CONTENT |
| 1996 | xmlNodeAddContent(cur, elem->content); |
| 1997 | #else |
| 1998 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1999 | #endif |
| 2000 | xmlFreeNode(elem); |
| 2001 | return(cur); |
| 2002 | } |
| 2003 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2004 | if (elem->doc != cur->doc) { |
| 2005 | xmlSetTreeDoc(elem, cur->doc); |
| 2006 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2007 | parent = cur->parent; |
| 2008 | elem->prev = cur; |
| 2009 | elem->next = NULL; |
| 2010 | elem->parent = parent; |
| 2011 | cur->next = elem; |
| 2012 | if (parent != NULL) |
| 2013 | parent->last = elem; |
| 2014 | |
| 2015 | return(elem); |
| 2016 | } |
| 2017 | |
| 2018 | /** |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2019 | * xmlAddChildList: |
| 2020 | * @parent: the parent node |
| 2021 | * @cur: the first node in the list |
| 2022 | * |
| 2023 | * Add a list of node at the end of the child list of the parent |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2024 | * merging adjacent TEXT nodes (@cur may be freed) |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2025 | * |
| 2026 | * Returns the last child or NULL in case of error. |
| 2027 | */ |
| 2028 | xmlNodePtr |
| 2029 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2030 | xmlNodePtr prev; |
| 2031 | |
| 2032 | if (parent == NULL) { |
| 2033 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2034 | xmlGenericError(xmlGenericErrorContext, |
| 2035 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2036 | #endif |
| 2037 | return(NULL); |
| 2038 | } |
| 2039 | |
| 2040 | if (cur == NULL) { |
| 2041 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2042 | xmlGenericError(xmlGenericErrorContext, |
| 2043 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2044 | #endif |
| 2045 | return(NULL); |
| 2046 | } |
| 2047 | |
| 2048 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2049 | (cur->doc != parent->doc)) { |
| 2050 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2051 | xmlGenericError(xmlGenericErrorContext, |
| 2052 | "Elements moved to a different document\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2053 | #endif |
| 2054 | } |
| 2055 | |
| 2056 | /* |
| 2057 | * add the first element at the end of the children list. |
| 2058 | */ |
| 2059 | if (parent->children == NULL) { |
| 2060 | parent->children = cur; |
| 2061 | } else { |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2062 | /* |
| 2063 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 2064 | */ |
| 2065 | if ((cur->type == XML_TEXT_NODE) && |
| 2066 | (parent->last->type == XML_TEXT_NODE)) { |
| 2067 | #ifndef XML_USE_BUFFER_CONTENT |
| 2068 | xmlNodeAddContent(parent->last, cur->content); |
| 2069 | #else |
| 2070 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2071 | #endif |
| 2072 | /* |
| 2073 | * if it's the only child, nothing more to be done. |
| 2074 | */ |
| 2075 | if (cur->next == NULL) { |
| 2076 | xmlFreeNode(cur); |
| 2077 | return(parent->last); |
| 2078 | } |
| 2079 | prev = cur; |
| 2080 | cur = cur->next; |
| 2081 | xmlFreeNode(prev); |
| 2082 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2083 | prev = parent->last; |
| 2084 | prev->next = cur; |
| 2085 | cur->prev = prev; |
| 2086 | } |
| 2087 | while (cur->next != NULL) { |
| 2088 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2089 | if (cur->doc != parent->doc) { |
| 2090 | xmlSetTreeDoc(cur, parent->doc); |
| 2091 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2092 | cur = cur->next; |
| 2093 | } |
| 2094 | cur->parent = parent; |
| 2095 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 2096 | parent->last = cur; |
| 2097 | |
| 2098 | return(cur); |
| 2099 | } |
| 2100 | |
| 2101 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2102 | * xmlAddChild: |
| 2103 | * @parent: the parent node |
| 2104 | * @cur: the child node |
| 2105 | * |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2106 | * Add a new child element, to @parent, at the end of the child list |
| 2107 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2108 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2109 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2110 | xmlNodePtr |
| 2111 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2112 | xmlNodePtr prev; |
| 2113 | |
| 2114 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2115 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2116 | xmlGenericError(xmlGenericErrorContext, |
| 2117 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2118 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2119 | return(NULL); |
| 2120 | } |
| 2121 | |
| 2122 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2123 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2124 | xmlGenericError(xmlGenericErrorContext, |
| 2125 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2126 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2127 | return(NULL); |
| 2128 | } |
| 2129 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2130 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2131 | (cur->doc != parent->doc)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2132 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2133 | xmlGenericError(xmlGenericErrorContext, |
| 2134 | "Elements moved to a different document\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2135 | #endif |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2138 | /* |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2139 | * If cur is a TEXT node, merge its content with adjacent TEXT nodes |
| 2140 | * or with parent->content if parent->content != NULL. |
| 2141 | * cur is then freed. |
| 2142 | */ |
| 2143 | if (cur->type == XML_TEXT_NODE) { |
| 2144 | if (((parent->type == XML_ELEMENT_NODE) || |
| 2145 | (parent->type == XML_TEXT_NODE)) && |
| 2146 | (parent->content != NULL)) { |
| 2147 | #ifndef XML_USE_BUFFER_CONTENT |
| 2148 | xmlNodeAddContent(parent, cur->content); |
| 2149 | #else |
| 2150 | xmlNodeAddContent(parent, xmlBufferContent(cur->content)); |
| 2151 | #endif |
| 2152 | xmlFreeNode(cur); |
| 2153 | return(parent); |
| 2154 | } |
| 2155 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE)) { |
| 2156 | #ifndef XML_USE_BUFFER_CONTENT |
| 2157 | xmlNodeAddContent(parent->last, cur->content); |
| 2158 | #else |
| 2159 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2160 | #endif |
| 2161 | xmlFreeNode(cur); |
| 2162 | return(parent->last); |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2167 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2168 | */ |
| 2169 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2170 | if (cur->doc != parent->doc) { |
| 2171 | xmlSetTreeDoc(cur, parent->doc); |
| 2172 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2173 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2174 | /* |
| 2175 | * Handle the case where parent->content != NULL, in that case it will |
| 2176 | * create a intermediate TEXT node. |
| 2177 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2178 | if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && |
| 2179 | (parent->content != NULL)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2180 | xmlNodePtr text; |
| 2181 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2182 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2183 | text = xmlNewDocText(parent->doc, parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2184 | #else |
| 2185 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 2186 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2187 | if (text != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2188 | text->next = parent->children; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2189 | if (text->next != NULL) |
| 2190 | text->next->prev = text; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2191 | parent->children = text; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2192 | UPDATE_LAST_CHILD_AND_PARENT(parent) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2193 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2194 | xmlFree(parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2195 | #else |
| 2196 | xmlBufferFree(parent->content); |
| 2197 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2198 | parent->content = NULL; |
| 2199 | } |
| 2200 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2201 | if (parent->children == NULL) { |
| 2202 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2203 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2204 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2205 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2206 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2207 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2208 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | return(cur); |
| 2212 | } |
| 2213 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2214 | /** |
| 2215 | * xmlGetLastChild: |
| 2216 | * @parent: the parent node |
| 2217 | * |
| 2218 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2219 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2220 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2221 | xmlNodePtr |
| 2222 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2223 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2224 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2225 | xmlGenericError(xmlGenericErrorContext, |
| 2226 | "xmlGetLastChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2227 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2228 | return(NULL); |
| 2229 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2230 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2233 | /** |
| 2234 | * xmlFreeNodeList: |
| 2235 | * @cur: the first node in the list |
| 2236 | * |
| 2237 | * 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] | 2238 | * the children are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2239 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2240 | void |
| 2241 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2242 | xmlNodePtr next; |
| 2243 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2244 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2245 | xmlGenericError(xmlGenericErrorContext, |
| 2246 | "xmlFreeNodeList : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2247 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2248 | return; |
| 2249 | } |
| 2250 | while (cur != NULL) { |
| 2251 | next = cur->next; |
| 2252 | xmlFreeNode(cur); |
| 2253 | cur = next; |
| 2254 | } |
| 2255 | } |
| 2256 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2257 | /** |
| 2258 | * xmlFreeNode: |
| 2259 | * @cur: the node |
| 2260 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2261 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 2262 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2263 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2264 | void |
| 2265 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2266 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2267 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2268 | xmlGenericError(xmlGenericErrorContext, |
| 2269 | "xmlFreeNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2270 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2271 | return; |
| 2272 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2273 | if (cur->type == XML_DTD_NODE) |
| 2274 | return; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2275 | cur->doc = NULL; |
| 2276 | cur->parent = NULL; |
| 2277 | cur->next = NULL; |
| 2278 | cur->prev = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2279 | if ((cur->children != NULL) && |
| 2280 | (cur->type != XML_ENTITY_REF_NODE)) |
| 2281 | xmlFreeNodeList(cur->children); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2282 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 2283 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2284 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2285 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2286 | #else |
| 2287 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 2288 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2289 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2290 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 2291 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2292 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2295 | /** |
| 2296 | * xmlUnlinkNode: |
| 2297 | * @cur: the node |
| 2298 | * |
| 2299 | * Unlink a node from it's current context, the node is not freed |
| 2300 | */ |
| 2301 | void |
| 2302 | xmlUnlinkNode(xmlNodePtr cur) { |
| 2303 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2304 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2305 | xmlGenericError(xmlGenericErrorContext, |
| 2306 | "xmlUnlinkNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2307 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2308 | return; |
| 2309 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2310 | if ((cur->parent != NULL) && (cur->parent->children == cur)) |
| 2311 | cur->parent->children = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2312 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 2313 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2314 | if (cur->next != NULL) |
| 2315 | cur->next->prev = cur->prev; |
| 2316 | if (cur->prev != NULL) |
| 2317 | cur->prev->next = cur->next; |
| 2318 | cur->next = cur->prev = NULL; |
| 2319 | cur->parent = NULL; |
| 2320 | } |
| 2321 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2322 | /** |
| 2323 | * xmlReplaceNode: |
| 2324 | * @old: the old node |
| 2325 | * @cur: the node |
| 2326 | * |
| 2327 | * Unlink the old node from it's current context, prune the new one |
| 2328 | * at the same place. If cur was already inserted in a document it is |
| 2329 | * first unlinked from its existing context. |
| 2330 | * |
| 2331 | * Returns the old node |
| 2332 | */ |
| 2333 | xmlNodePtr |
| 2334 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 2335 | if (old == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2336 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2337 | xmlGenericError(xmlGenericErrorContext, |
| 2338 | "xmlReplaceNode : old == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2339 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2340 | return(NULL); |
| 2341 | } |
| 2342 | if (cur == NULL) { |
| 2343 | xmlUnlinkNode(old); |
| 2344 | return(old); |
| 2345 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2346 | if (cur == old) { |
| 2347 | return(old); |
| 2348 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2349 | xmlUnlinkNode(cur); |
| 2350 | cur->doc = old->doc; |
| 2351 | cur->parent = old->parent; |
| 2352 | cur->next = old->next; |
| 2353 | if (cur->next != NULL) |
| 2354 | cur->next->prev = cur; |
| 2355 | cur->prev = old->prev; |
| 2356 | if (cur->prev != NULL) |
| 2357 | cur->prev->next = cur; |
| 2358 | if (cur->parent != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2359 | if (cur->parent->children == old) |
| 2360 | cur->parent->children = cur; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2361 | if (cur->parent->last == old) |
| 2362 | cur->parent->last = cur; |
| 2363 | } |
| 2364 | old->next = old->prev = NULL; |
| 2365 | old->parent = NULL; |
| 2366 | return(old); |
| 2367 | } |
| 2368 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2369 | /************************************************************************ |
| 2370 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2371 | * Copy operations * |
| 2372 | * * |
| 2373 | ************************************************************************/ |
| 2374 | |
| 2375 | /** |
| 2376 | * xmlCopyNamespace: |
| 2377 | * @cur: the namespace |
| 2378 | * |
| 2379 | * Do a copy of the namespace. |
| 2380 | * |
| 2381 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2382 | */ |
| 2383 | xmlNsPtr |
| 2384 | xmlCopyNamespace(xmlNsPtr cur) { |
| 2385 | xmlNsPtr ret; |
| 2386 | |
| 2387 | if (cur == NULL) return(NULL); |
| 2388 | switch (cur->type) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2389 | case XML_LOCAL_NAMESPACE: |
| 2390 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 2391 | break; |
| 2392 | default: |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2393 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2394 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2395 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2396 | #endif |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2397 | return(NULL); |
| 2398 | } |
| 2399 | return(ret); |
| 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlCopyNamespaceList: |
| 2404 | * @cur: the first namespace |
| 2405 | * |
| 2406 | * Do a copy of an namespace list. |
| 2407 | * |
| 2408 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2409 | */ |
| 2410 | xmlNsPtr |
| 2411 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 2412 | xmlNsPtr ret = NULL; |
| 2413 | xmlNsPtr p = NULL,q; |
| 2414 | |
| 2415 | while (cur != NULL) { |
| 2416 | q = xmlCopyNamespace(cur); |
| 2417 | if (p == NULL) { |
| 2418 | ret = p = q; |
| 2419 | } else { |
| 2420 | p->next = q; |
| 2421 | p = q; |
| 2422 | } |
| 2423 | cur = cur->next; |
| 2424 | } |
| 2425 | return(ret); |
| 2426 | } |
| 2427 | |
| 2428 | /** |
| 2429 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2430 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2431 | * @cur: the attribute |
| 2432 | * |
| 2433 | * Do a copy of the attribute. |
| 2434 | * |
| 2435 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2436 | */ |
| 2437 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2438 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2439 | xmlAttrPtr ret; |
| 2440 | |
| 2441 | if (cur == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2442 | if (cur->parent != NULL) |
| 2443 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 2444 | else if (cur->children != NULL) |
| 2445 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2446 | else |
| 2447 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2448 | if (ret == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2449 | ret->parent = target; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2450 | |
| 2451 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2452 | xmlNsPtr ns; |
| 2453 | |
| 2454 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2455 | ret->ns = ns; |
| 2456 | } else |
| 2457 | ret->ns = NULL; |
| 2458 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2459 | if (cur->children != NULL) { |
| 2460 | xmlNodePtr tmp; |
| 2461 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2462 | ret->children = xmlCopyNodeList(cur->children); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2463 | ret->last = NULL; |
| 2464 | tmp = ret->children; |
| 2465 | while (tmp != NULL) { |
| 2466 | tmp->parent = (xmlNodePtr)ret; |
| 2467 | if (tmp->next == NULL) |
| 2468 | ret->last = tmp; |
| 2469 | tmp = tmp->next; |
| 2470 | } |
| 2471 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2472 | return(ret); |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2477 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2478 | * @cur: the first attribute |
| 2479 | * |
| 2480 | * Do a copy of an attribute list. |
| 2481 | * |
| 2482 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2483 | */ |
| 2484 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2485 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2486 | xmlAttrPtr ret = NULL; |
| 2487 | xmlAttrPtr p = NULL,q; |
| 2488 | |
| 2489 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2490 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2491 | if (p == NULL) { |
| 2492 | ret = p = q; |
| 2493 | } else { |
| 2494 | p->next = q; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2495 | q->prev = p; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2496 | p = q; |
| 2497 | } |
| 2498 | cur = cur->next; |
| 2499 | } |
| 2500 | return(ret); |
| 2501 | } |
| 2502 | |
| 2503 | /* |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 2504 | * NOTE abeut the CopyNode operations ! |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2505 | * |
| 2506 | * They are splitted into external and internal parts for one |
| 2507 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2508 | * say RPM:Copyright without changing the namespace pointer to |
| 2509 | * something else can produce stale links. One way to do it is |
| 2510 | * to keep a reference counter but this doesn't work as soon |
| 2511 | * as one move the element or the subtree out of the scope of |
| 2512 | * the existing namespace. The actual solution seems to add |
| 2513 | * a copy of the namespace at the top of the copied tree if |
| 2514 | * not available in the subtree. |
| 2515 | * Hence two functions, the public front-end call the inner ones |
| 2516 | */ |
| 2517 | |
| 2518 | static xmlNodePtr |
| 2519 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2520 | |
| 2521 | static xmlNodePtr |
| 2522 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2523 | int recursive) { |
| 2524 | xmlNodePtr ret; |
| 2525 | |
| 2526 | if (node == NULL) return(NULL); |
| 2527 | /* |
| 2528 | * Allocate a new node and fill the fields. |
| 2529 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2530 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2531 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2532 | xmlGenericError(xmlGenericErrorContext, |
| 2533 | "xmlStaticCopyNode : malloc failed\n"); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2534 | return(NULL); |
| 2535 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2536 | memset(ret, 0, sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2537 | ret->type = node->type; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2538 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2539 | ret->doc = doc; |
| 2540 | ret->parent = parent; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2541 | if (node->name != NULL) |
| 2542 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2543 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2544 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2545 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2546 | #else |
| 2547 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2548 | xmlBufferSetAllocationScheme(ret->content, |
| 2549 | xmlGetBufferAllocationScheme()); |
| 2550 | xmlBufferAdd(ret->content, |
| 2551 | xmlBufferContent(node->content), |
| 2552 | xmlBufferLength(node->content)); |
| 2553 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2554 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2555 | if (parent != NULL) |
| 2556 | xmlAddChild(parent, ret); |
| 2557 | |
| 2558 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2559 | if (node->nsDef != NULL) |
| 2560 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2561 | |
| 2562 | if (node->ns != NULL) { |
| 2563 | xmlNsPtr ns; |
| 2564 | |
| 2565 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2566 | if (ns == NULL) { |
| 2567 | /* |
| 2568 | * Humm, we are copying an element whose namespace is defined |
| 2569 | * out of the new tree scope. Search it in the original tree |
| 2570 | * and add it at the top of the new tree |
| 2571 | */ |
| 2572 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2573 | if (ns != NULL) { |
| 2574 | xmlNodePtr root = ret; |
| 2575 | |
| 2576 | while (root->parent != NULL) root = root->parent; |
| 2577 | xmlNewNs(root, ns->href, ns->prefix); |
| 2578 | } |
| 2579 | } else { |
| 2580 | /* |
| 2581 | * reference the existing namespace definition in our own tree. |
| 2582 | */ |
| 2583 | ret->ns = ns; |
| 2584 | } |
| 2585 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2586 | if (node->properties != NULL) |
| 2587 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2588 | if (node->children != NULL) |
| 2589 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2590 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2591 | return(ret); |
| 2592 | } |
| 2593 | |
| 2594 | static xmlNodePtr |
| 2595 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2596 | xmlNodePtr ret = NULL; |
| 2597 | xmlNodePtr p = NULL,q; |
| 2598 | |
| 2599 | while (node != NULL) { |
| 2600 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2601 | if (ret == NULL) { |
| 2602 | q->prev = NULL; |
| 2603 | ret = p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2604 | } else { |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2605 | p->next = q; |
| 2606 | q->prev = p; |
| 2607 | p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2608 | } |
| 2609 | node = node->next; |
| 2610 | } |
| 2611 | return(ret); |
| 2612 | } |
| 2613 | |
| 2614 | /** |
| 2615 | * xmlCopyNode: |
| 2616 | * @node: the node |
| 2617 | * @recursive: if 1 do a recursive copy. |
| 2618 | * |
| 2619 | * Do a copy of the node. |
| 2620 | * |
| 2621 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2622 | */ |
| 2623 | xmlNodePtr |
| 2624 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2625 | xmlNodePtr ret; |
| 2626 | |
| 2627 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2628 | return(ret); |
| 2629 | } |
| 2630 | |
| 2631 | /** |
| 2632 | * xmlCopyNodeList: |
| 2633 | * @node: the first node in the list. |
| 2634 | * |
| 2635 | * Do a recursive copy of the node list. |
| 2636 | * |
| 2637 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2638 | */ |
| 2639 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2640 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2641 | return(ret); |
| 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * xmlCopyElement: |
| 2646 | * @elem: the element |
| 2647 | * |
| 2648 | * Do a copy of the element definition. |
| 2649 | * |
| 2650 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2651 | xmlElementPtr |
| 2652 | xmlCopyElement(xmlElementPtr elem) { |
| 2653 | xmlElementPtr ret; |
| 2654 | |
| 2655 | if (elem == NULL) return(NULL); |
| 2656 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2657 | if (ret == NULL) return(NULL); |
| 2658 | if (!recursive) return(ret); |
| 2659 | if (elem->properties != NULL) |
| 2660 | ret->properties = xmlCopyPropList(elem->properties); |
| 2661 | |
| 2662 | if (elem->nsDef != NULL) |
| 2663 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2664 | if (elem->children != NULL) |
| 2665 | ret->children = xmlCopyElementList(elem->children); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2666 | return(ret); |
| 2667 | } |
| 2668 | */ |
| 2669 | |
| 2670 | /** |
| 2671 | * xmlCopyDtd: |
| 2672 | * @dtd: the dtd |
| 2673 | * |
| 2674 | * Do a copy of the dtd. |
| 2675 | * |
| 2676 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2677 | */ |
| 2678 | xmlDtdPtr |
| 2679 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2680 | xmlDtdPtr ret; |
| 2681 | |
| 2682 | if (dtd == NULL) return(NULL); |
| 2683 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2684 | if (ret == NULL) return(NULL); |
| 2685 | if (dtd->entities != NULL) |
| 2686 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2687 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2688 | if (dtd->notations != NULL) |
| 2689 | ret->notations = (void *) xmlCopyNotationTable( |
| 2690 | (xmlNotationTablePtr) dtd->notations); |
| 2691 | if (dtd->elements != NULL) |
| 2692 | ret->elements = (void *) xmlCopyElementTable( |
| 2693 | (xmlElementTablePtr) dtd->elements); |
| 2694 | if (dtd->attributes != NULL) |
| 2695 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2696 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2697 | return(ret); |
| 2698 | } |
| 2699 | |
| 2700 | /** |
| 2701 | * xmlCopyDoc: |
| 2702 | * @doc: the document |
| 2703 | * @recursive: if 1 do a recursive copy. |
| 2704 | * |
| 2705 | * Do a copy of the document info. If recursive, the content tree will |
| 2706 | * be copied too as well as Dtd, namespaces and entities. |
| 2707 | * |
| 2708 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2709 | */ |
| 2710 | xmlDocPtr |
| 2711 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2712 | xmlDocPtr ret; |
| 2713 | |
| 2714 | if (doc == NULL) return(NULL); |
| 2715 | ret = xmlNewDoc(doc->version); |
| 2716 | if (ret == NULL) return(NULL); |
| 2717 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2718 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2719 | if (doc->encoding != NULL) |
| 2720 | ret->encoding = xmlStrdup(doc->encoding); |
| 2721 | ret->compression = doc->compression; |
| 2722 | ret->standalone = doc->standalone; |
| 2723 | if (!recursive) return(ret); |
| 2724 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2725 | if (doc->intSubset != NULL) |
| 2726 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2727 | if (doc->oldNs != NULL) |
| 2728 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2729 | if (doc->children != NULL) { |
| 2730 | xmlNodePtr tmp; |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2731 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 2732 | (xmlNodePtr)ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2733 | ret->last = NULL; |
| 2734 | tmp = ret->children; |
| 2735 | while (tmp != NULL) { |
| 2736 | if (tmp->next == NULL) |
| 2737 | ret->last = tmp; |
| 2738 | tmp = tmp->next; |
| 2739 | } |
| 2740 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2741 | return(ret); |
| 2742 | } |
| 2743 | |
| 2744 | /************************************************************************ |
| 2745 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2746 | * Content access functions * |
| 2747 | * * |
| 2748 | ************************************************************************/ |
| 2749 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2750 | /** |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2751 | * xmlDocGetRootElement: |
| 2752 | * @doc: the document |
| 2753 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2754 | * Get the root element of the document (doc->children is a list |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2755 | * containing possibly comments, PIs, etc ...). |
| 2756 | * |
| 2757 | * Returns the xmlNodePtr for the root or NULL |
| 2758 | */ |
| 2759 | xmlNodePtr |
| 2760 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2761 | xmlNodePtr ret; |
| 2762 | |
| 2763 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2764 | ret = doc->children; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2765 | while (ret != NULL) { |
| 2766 | if (ret->type == XML_ELEMENT_NODE) |
| 2767 | return(ret); |
| 2768 | ret = ret->next; |
| 2769 | } |
| 2770 | return(ret); |
| 2771 | } |
| 2772 | |
| 2773 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2774 | * xmlDocSetRootElement: |
| 2775 | * @doc: the document |
| 2776 | * @root: the new document root element |
| 2777 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2778 | * Set the root element of the document (doc->children is a list |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2779 | * containing possibly comments, PIs, etc ...). |
| 2780 | * |
| 2781 | * Returns the old root element if any was found |
| 2782 | */ |
| 2783 | xmlNodePtr |
| 2784 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2785 | xmlNodePtr old = NULL; |
| 2786 | |
| 2787 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2788 | old = doc->children; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2789 | while (old != NULL) { |
| 2790 | if (old->type == XML_ELEMENT_NODE) |
| 2791 | break; |
| 2792 | old = old->next; |
| 2793 | } |
| 2794 | if (old == NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2795 | if (doc->children == NULL) { |
| 2796 | doc->children = root; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2797 | doc->last = root; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2798 | } else { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2799 | xmlAddSibling(doc->children, root); |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2800 | } |
| 2801 | } else { |
| 2802 | xmlReplaceNode(old, root); |
| 2803 | } |
| 2804 | return(old); |
| 2805 | } |
| 2806 | |
| 2807 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2808 | * xmlNodeSetLang: |
| 2809 | * @cur: the node being changed |
| 2810 | * @lang: the langage description |
| 2811 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2812 | * Set the language of a node, i.e. the values of the xml:lang |
| 2813 | * attribute. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2814 | */ |
| 2815 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2816 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2817 | if (cur == NULL) return; |
| 2818 | switch(cur->type) { |
| 2819 | case XML_TEXT_NODE: |
| 2820 | case XML_CDATA_SECTION_NODE: |
| 2821 | case XML_COMMENT_NODE: |
| 2822 | case XML_DOCUMENT_NODE: |
| 2823 | case XML_DOCUMENT_TYPE_NODE: |
| 2824 | case XML_DOCUMENT_FRAG_NODE: |
| 2825 | case XML_NOTATION_NODE: |
| 2826 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2827 | case XML_DTD_NODE: |
| 2828 | case XML_ELEMENT_DECL: |
| 2829 | case XML_ATTRIBUTE_DECL: |
| 2830 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2831 | case XML_PI_NODE: |
| 2832 | case XML_ENTITY_REF_NODE: |
| 2833 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2834 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2835 | #ifdef LIBXML_SGML_ENABLED |
| 2836 | case XML_SGML_DOCUMENT_NODE: |
| 2837 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2838 | case XML_XINCLUDE_START: |
| 2839 | case XML_XINCLUDE_END: |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 2840 | return; |
| 2841 | case XML_ELEMENT_NODE: |
| 2842 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2843 | break; |
| 2844 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2845 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2846 | } |
| 2847 | |
| 2848 | /** |
| 2849 | * xmlNodeGetLang: |
| 2850 | * @cur: the node being checked |
| 2851 | * |
| 2852 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2853 | * attribute or the one carried by the nearest ancestor. |
| 2854 | * |
| 2855 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2856 | * It's up to the caller to free the memory. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2857 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2858 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2859 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2860 | xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2861 | |
| 2862 | while (cur != NULL) { |
| 2863 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2864 | if (lang != NULL) |
| 2865 | return(lang); |
| 2866 | cur = cur->parent; |
| 2867 | } |
| 2868 | return(NULL); |
| 2869 | } |
| 2870 | |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2871 | |
| 2872 | /** |
| 2873 | * xmlNodeSetSpacePreserve: |
| 2874 | * @cur: the node being changed |
| 2875 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 2876 | * |
| 2877 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 2878 | * value of the xml:space attribute. |
| 2879 | */ |
| 2880 | void |
| 2881 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
| 2882 | if (cur == NULL) return; |
| 2883 | switch(cur->type) { |
| 2884 | case XML_TEXT_NODE: |
| 2885 | case XML_CDATA_SECTION_NODE: |
| 2886 | case XML_COMMENT_NODE: |
| 2887 | case XML_DOCUMENT_NODE: |
| 2888 | case XML_DOCUMENT_TYPE_NODE: |
| 2889 | case XML_DOCUMENT_FRAG_NODE: |
| 2890 | case XML_NOTATION_NODE: |
| 2891 | case XML_HTML_DOCUMENT_NODE: |
| 2892 | case XML_DTD_NODE: |
| 2893 | case XML_ELEMENT_DECL: |
| 2894 | case XML_ATTRIBUTE_DECL: |
| 2895 | case XML_ENTITY_DECL: |
| 2896 | case XML_PI_NODE: |
| 2897 | case XML_ENTITY_REF_NODE: |
| 2898 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2899 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2900 | case XML_XINCLUDE_START: |
| 2901 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2902 | #ifdef LIBXML_SGML_ENABLED |
| 2903 | case XML_SGML_DOCUMENT_NODE: |
| 2904 | #endif |
| 2905 | return; |
| 2906 | case XML_ELEMENT_NODE: |
| 2907 | case XML_ATTRIBUTE_NODE: |
| 2908 | break; |
| 2909 | } |
| 2910 | switch (val) { |
| 2911 | case 0: |
| 2912 | xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default"); |
| 2913 | break; |
| 2914 | case 1: |
| 2915 | xmlSetProp(cur, BAD_CAST "xml:space", |
| 2916 | BAD_CAST "preserve"); |
| 2917 | break; |
| 2918 | } |
| 2919 | } |
| 2920 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2921 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2922 | * xmlNodeGetSpacePreserve: |
| 2923 | * @cur: the node being checked |
| 2924 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2925 | * Searches the space preserving behaviour of a node, i.e. the values |
| 2926 | * of the xml:space attribute or the one carried by the nearest |
| 2927 | * ancestor. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2928 | * |
| 2929 | * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve" |
| 2930 | */ |
| 2931 | int |
| 2932 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 2933 | xmlChar *space; |
| 2934 | |
| 2935 | while (cur != NULL) { |
| 2936 | space = xmlGetProp(cur, BAD_CAST "xml:space"); |
| 2937 | if (space != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2938 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2939 | xmlFree(space); |
| 2940 | return(1); |
| 2941 | } |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2942 | if (xmlStrEqual(space, BAD_CAST "default")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2943 | xmlFree(space); |
| 2944 | return(0); |
| 2945 | } |
| 2946 | xmlFree(space); |
| 2947 | } |
| 2948 | cur = cur->parent; |
| 2949 | } |
| 2950 | return(-1); |
| 2951 | } |
| 2952 | |
| 2953 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2954 | * xmlNodeSetName: |
| 2955 | * @cur: the node being changed |
| 2956 | * @name: the new tag name |
| 2957 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2958 | * Set (or reset) the name of a node. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2959 | */ |
| 2960 | void |
| 2961 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2962 | if (cur == NULL) return; |
| 2963 | if (name == NULL) return; |
| 2964 | switch(cur->type) { |
| 2965 | case XML_TEXT_NODE: |
| 2966 | case XML_CDATA_SECTION_NODE: |
| 2967 | case XML_COMMENT_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2968 | case XML_DOCUMENT_TYPE_NODE: |
| 2969 | case XML_DOCUMENT_FRAG_NODE: |
| 2970 | case XML_NOTATION_NODE: |
| 2971 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2972 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2973 | case XML_XINCLUDE_START: |
| 2974 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2975 | #ifdef LIBXML_SGML_ENABLED |
| 2976 | case XML_SGML_DOCUMENT_NODE: |
| 2977 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2978 | return; |
| 2979 | case XML_ELEMENT_NODE: |
| 2980 | case XML_ATTRIBUTE_NODE: |
| 2981 | case XML_PI_NODE: |
| 2982 | case XML_ENTITY_REF_NODE: |
| 2983 | case XML_ENTITY_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2984 | case XML_DTD_NODE: |
| 2985 | case XML_DOCUMENT_NODE: |
| 2986 | case XML_ELEMENT_DECL: |
| 2987 | case XML_ATTRIBUTE_DECL: |
| 2988 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2989 | break; |
| 2990 | } |
| 2991 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 2992 | cur->name = xmlStrdup(name); |
| 2993 | } |
| 2994 | |
| 2995 | /** |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2996 | * xmlNodeSetBase: |
| 2997 | * @cur: the node being changed |
| 2998 | * @uri: the new base URI |
| 2999 | * |
| 3000 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 3001 | * xml:base attribute. |
| 3002 | */ |
| 3003 | void |
| 3004 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
| 3005 | if (cur == NULL) return; |
| 3006 | switch(cur->type) { |
| 3007 | case XML_TEXT_NODE: |
| 3008 | case XML_CDATA_SECTION_NODE: |
| 3009 | case XML_COMMENT_NODE: |
| 3010 | case XML_DOCUMENT_NODE: |
| 3011 | case XML_DOCUMENT_TYPE_NODE: |
| 3012 | case XML_DOCUMENT_FRAG_NODE: |
| 3013 | case XML_NOTATION_NODE: |
| 3014 | case XML_HTML_DOCUMENT_NODE: |
| 3015 | case XML_DTD_NODE: |
| 3016 | case XML_ELEMENT_DECL: |
| 3017 | case XML_ATTRIBUTE_DECL: |
| 3018 | case XML_ENTITY_DECL: |
| 3019 | case XML_PI_NODE: |
| 3020 | case XML_ENTITY_REF_NODE: |
| 3021 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3022 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3023 | case XML_XINCLUDE_START: |
| 3024 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 3025 | #ifdef LIBXML_SGML_ENABLED |
| 3026 | case XML_SGML_DOCUMENT_NODE: |
| 3027 | #endif |
| 3028 | return; |
| 3029 | case XML_ELEMENT_NODE: |
| 3030 | case XML_ATTRIBUTE_NODE: |
| 3031 | break; |
| 3032 | } |
| 3033 | xmlSetProp(cur, BAD_CAST "xml:base", uri); |
| 3034 | } |
| 3035 | |
| 3036 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3037 | * xmlDocumentGetBase: |
| 3038 | * @doc: the document |
| 3039 | * |
| 3040 | * Searches for the Document BASE URL. The code should work on both XML |
| 3041 | * and HTML document. |
| 3042 | * It returns the base as defined in RFC 2396 section |
| 3043 | * 5.1.3. Base URI from the Retrieval URI |
| 3044 | * However it does not return the computed base (5.1.1 and 5.1.2), use |
| 3045 | * xmlNodeGetBase() for this |
| 3046 | * |
| 3047 | * Returns a pointer to the base URL, or NULL if not found |
| 3048 | * It's up to the caller to free the memory. |
| 3049 | */ |
| 3050 | xmlChar * |
| 3051 | xmlDocumentGetBase(xmlDocPtr doc) { |
| 3052 | if (doc == NULL) |
| 3053 | return(NULL); |
| 3054 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 3055 | if (doc->URL != NULL) |
| 3056 | return(xmlStrdup(doc->URL)); |
| 3057 | return(NULL); |
| 3058 | } |
| 3059 | if (doc->URL != NULL) |
| 3060 | return(xmlStrdup(doc->URL)); |
| 3061 | return(NULL); |
| 3062 | } |
| 3063 | |
| 3064 | /** |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3065 | * xmlNodeGetBase: |
| 3066 | * @doc: the document the node pertains to |
| 3067 | * @cur: the node being checked |
| 3068 | * |
| 3069 | * Searches for the BASE URL. The code should work on both XML |
| 3070 | * and HTML document even if base mechanisms are completely different. |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3071 | * It returns the base as defined in RFC 2396 sections |
| 3072 | * 5.1.1. Base URI within Document Content |
| 3073 | * and |
| 3074 | * 5.1.2. Base URI from the Encapsulating Entity |
| 3075 | * However it does not return the document base (5.1.3), use |
| 3076 | * xmlDocumentGetBase() for this |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3077 | * |
| 3078 | * Returns a pointer to the base URL, or NULL if not found |
| 3079 | * It's up to the caller to free the memory. |
| 3080 | */ |
| 3081 | xmlChar * |
| 3082 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 3083 | xmlChar *base; |
| 3084 | |
| 3085 | if ((cur == NULL) && (doc == NULL)) |
| 3086 | return(NULL); |
| 3087 | if (doc == NULL) doc = cur->doc; |
| 3088 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3089 | cur = doc->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3090 | while ((cur != NULL) && (cur->name != NULL)) { |
| 3091 | if (cur->type != XML_ELEMENT_NODE) { |
| 3092 | cur = cur->next; |
| 3093 | continue; |
| 3094 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3095 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3096 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3097 | continue; |
| 3098 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3099 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3100 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3101 | continue; |
| 3102 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3103 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 3104 | return(xmlGetProp(cur, BAD_CAST "href")); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3105 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3106 | cur = cur->next; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3107 | } |
| 3108 | return(NULL); |
| 3109 | } |
| 3110 | while (cur != NULL) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3111 | if (cur->type == XML_ENTITY_DECL) { |
| 3112 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3113 | return(xmlStrdup(ent->URI)); |
| 3114 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3115 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 3116 | if (base != NULL) |
| 3117 | return(base); |
| 3118 | cur = cur->parent; |
| 3119 | } |
| 3120 | return(NULL); |
| 3121 | } |
| 3122 | |
| 3123 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3124 | * xmlNodeGetContent: |
| 3125 | * @cur: the node being read |
| 3126 | * |
| 3127 | * Read the value of a node, this can be either the text carried |
| 3128 | * directly by this node if it's a TEXT node or the aggregate string |
| 3129 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 3130 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3131 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3132 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3133 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3134 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3135 | xmlNodeGetContent(xmlNodePtr cur) { |
| 3136 | if (cur == NULL) return(NULL); |
| 3137 | switch (cur->type) { |
| 3138 | case XML_DOCUMENT_FRAG_NODE: |
| 3139 | case XML_ELEMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3140 | return(xmlNodeListGetString(cur->doc, cur->children, 1)); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3141 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3142 | case XML_ATTRIBUTE_NODE: { |
| 3143 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3144 | if (attr->parent != NULL) |
| 3145 | return(xmlNodeListGetString(attr->parent->doc, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3146 | else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3147 | return(xmlNodeListGetString(NULL, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3148 | break; |
| 3149 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3150 | case XML_COMMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3151 | case XML_PI_NODE: |
| 3152 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3153 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3154 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3155 | #else |
| 3156 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3157 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3158 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3159 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3160 | /* |
| 3161 | * Locate the entity, and get it's content |
| 3162 | * @@@ |
| 3163 | */ |
| 3164 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3165 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3166 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3167 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3168 | case XML_DOCUMENT_TYPE_NODE: |
| 3169 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3170 | case XML_DTD_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3171 | case XML_XINCLUDE_START: |
| 3172 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3173 | #ifdef LIBXML_SGML_ENABLED |
| 3174 | case XML_SGML_DOCUMENT_NODE: |
| 3175 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3176 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3177 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3178 | return(xmlStrdup(((xmlNsPtr)cur)->href)); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3179 | case XML_ELEMENT_DECL: |
| 3180 | /* TODO !!! */ |
| 3181 | return(NULL); |
| 3182 | case XML_ATTRIBUTE_DECL: |
| 3183 | /* TODO !!! */ |
| 3184 | return(NULL); |
| 3185 | case XML_ENTITY_DECL: |
| 3186 | /* TODO !!! */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3187 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3188 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3189 | case XML_TEXT_NODE: |
| 3190 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3191 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3192 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3193 | #else |
| 3194 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3195 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3196 | return(NULL); |
| 3197 | } |
| 3198 | return(NULL); |
| 3199 | } |
| 3200 | |
| 3201 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3202 | * xmlNodeSetContent: |
| 3203 | * @cur: the node being modified |
| 3204 | * @content: the new value of the content |
| 3205 | * |
| 3206 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3207 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3208 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3209 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3210 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3211 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3212 | xmlGenericError(xmlGenericErrorContext, |
| 3213 | "xmlNodeSetContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3214 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3215 | return; |
| 3216 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3217 | switch (cur->type) { |
| 3218 | case XML_DOCUMENT_FRAG_NODE: |
| 3219 | case XML_ELEMENT_NODE: |
| 3220 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3221 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3222 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3223 | #else |
| 3224 | xmlBufferFree(cur->content); |
| 3225 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3226 | cur->content = NULL; |
| 3227 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3228 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3229 | cur->children = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3230 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | case XML_ATTRIBUTE_NODE: |
| 3233 | break; |
| 3234 | case XML_TEXT_NODE: |
| 3235 | case XML_CDATA_SECTION_NODE: |
| 3236 | case XML_ENTITY_REF_NODE: |
| 3237 | case XML_ENTITY_NODE: |
| 3238 | case XML_PI_NODE: |
| 3239 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3240 | if (cur->content != NULL) { |
| 3241 | #ifndef XML_USE_BUFFER_CONTENT |
| 3242 | xmlFree(cur->content); |
| 3243 | #else |
| 3244 | xmlBufferFree(cur->content); |
| 3245 | #endif |
| 3246 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3247 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3248 | cur->last = cur->children = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3249 | if (content != NULL) { |
| 3250 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3251 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3252 | #else |
| 3253 | cur->content = xmlBufferCreateSize(0); |
| 3254 | xmlBufferSetAllocationScheme(cur->content, |
| 3255 | xmlGetBufferAllocationScheme()); |
| 3256 | xmlBufferAdd(cur->content, content, -1); |
| 3257 | #endif |
| 3258 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3259 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3260 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3261 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3262 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3263 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3264 | case XML_XINCLUDE_START: |
| 3265 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3266 | #ifdef LIBXML_SGML_ENABLED |
| 3267 | case XML_SGML_DOCUMENT_NODE: |
| 3268 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3269 | break; |
| 3270 | case XML_NOTATION_NODE: |
| 3271 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3272 | case XML_DTD_NODE: |
| 3273 | break; |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3274 | case XML_NAMESPACE_DECL: |
| 3275 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3276 | case XML_ELEMENT_DECL: |
| 3277 | /* TODO !!! */ |
| 3278 | break; |
| 3279 | case XML_ATTRIBUTE_DECL: |
| 3280 | /* TODO !!! */ |
| 3281 | break; |
| 3282 | case XML_ENTITY_DECL: |
| 3283 | /* TODO !!! */ |
| 3284 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3285 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3288 | /** |
| 3289 | * xmlNodeSetContentLen: |
| 3290 | * @cur: the node being modified |
| 3291 | * @content: the new value of the content |
| 3292 | * @len: the size of @content |
| 3293 | * |
| 3294 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3295 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3296 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3297 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3298 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3299 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3300 | xmlGenericError(xmlGenericErrorContext, |
| 3301 | "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3302 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3303 | return; |
| 3304 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3305 | switch (cur->type) { |
| 3306 | case XML_DOCUMENT_FRAG_NODE: |
| 3307 | case XML_ELEMENT_NODE: |
| 3308 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3309 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3310 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3311 | #else |
| 3312 | xmlBufferFree(cur->content); |
| 3313 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3314 | cur->content = NULL; |
| 3315 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3316 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3317 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3318 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3319 | break; |
| 3320 | case XML_ATTRIBUTE_NODE: |
| 3321 | break; |
| 3322 | case XML_TEXT_NODE: |
| 3323 | case XML_CDATA_SECTION_NODE: |
| 3324 | case XML_ENTITY_REF_NODE: |
| 3325 | case XML_ENTITY_NODE: |
| 3326 | case XML_PI_NODE: |
| 3327 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3328 | case XML_NOTATION_NODE: |
| 3329 | if (cur->content != NULL) { |
| 3330 | #ifndef XML_USE_BUFFER_CONTENT |
| 3331 | xmlFree(cur->content); |
| 3332 | #else |
| 3333 | xmlBufferFree(cur->content); |
| 3334 | #endif |
| 3335 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3336 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3337 | cur->children = cur->last = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3338 | if (content != NULL) { |
| 3339 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3340 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3341 | #else |
| 3342 | cur->content = xmlBufferCreateSize(len); |
| 3343 | xmlBufferSetAllocationScheme(cur->content, |
| 3344 | xmlGetBufferAllocationScheme()); |
| 3345 | xmlBufferAdd(cur->content, content, len); |
| 3346 | #endif |
| 3347 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3348 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3349 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3350 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3351 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3352 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3353 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3354 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3355 | case XML_XINCLUDE_START: |
| 3356 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3357 | #ifdef LIBXML_SGML_ENABLED |
| 3358 | case XML_SGML_DOCUMENT_NODE: |
| 3359 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3360 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3361 | case XML_ELEMENT_DECL: |
| 3362 | /* TODO !!! */ |
| 3363 | break; |
| 3364 | case XML_ATTRIBUTE_DECL: |
| 3365 | /* TODO !!! */ |
| 3366 | break; |
| 3367 | case XML_ENTITY_DECL: |
| 3368 | /* TODO !!! */ |
| 3369 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3370 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3373 | /** |
| 3374 | * xmlNodeAddContentLen: |
| 3375 | * @cur: the node being modified |
| 3376 | * @content: extra content |
| 3377 | * @len: the size of @content |
| 3378 | * |
| 3379 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3380 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3381 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3382 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3383 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3384 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3385 | xmlGenericError(xmlGenericErrorContext, |
| 3386 | "xmlNodeAddContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3387 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3388 | return; |
| 3389 | } |
| 3390 | if (len <= 0) return; |
| 3391 | switch (cur->type) { |
| 3392 | case XML_DOCUMENT_FRAG_NODE: |
| 3393 | case XML_ELEMENT_NODE: { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3394 | xmlNodePtr last = NULL, newNode; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3395 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3396 | if (cur->children != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3397 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3398 | } else { |
| 3399 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3400 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3401 | cur->children = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3402 | #else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3403 | cur->children = xmlStringGetNodeList(cur->doc, |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3404 | xmlBufferContent(cur->content)); |
| 3405 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3406 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3407 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3408 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3409 | #else |
| 3410 | xmlBufferFree(cur->content); |
| 3411 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3412 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3413 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3414 | } |
| 3415 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3416 | newNode = xmlNewTextLen(content, len); |
| 3417 | if (newNode != NULL) { |
| 3418 | xmlAddChild(cur, newNode); |
| 3419 | if ((last != NULL) && (last->next == newNode)) { |
| 3420 | xmlTextMerge(last, newNode); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3421 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3422 | } |
| 3423 | break; |
| 3424 | } |
| 3425 | case XML_ATTRIBUTE_NODE: |
| 3426 | break; |
| 3427 | case XML_TEXT_NODE: |
| 3428 | case XML_CDATA_SECTION_NODE: |
| 3429 | case XML_ENTITY_REF_NODE: |
| 3430 | case XML_ENTITY_NODE: |
| 3431 | case XML_PI_NODE: |
| 3432 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3433 | case XML_NOTATION_NODE: |
| 3434 | if (content != NULL) { |
| 3435 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3436 | cur->content = xmlStrncat(cur->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3437 | #else |
| 3438 | xmlBufferAdd(cur->content, content, len); |
| 3439 | #endif |
| 3440 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3441 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3442 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3443 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3444 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3445 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3446 | case XML_XINCLUDE_START: |
| 3447 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3448 | #ifdef LIBXML_SGML_ENABLED |
| 3449 | case XML_SGML_DOCUMENT_NODE: |
| 3450 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3451 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3452 | case XML_ELEMENT_DECL: |
| 3453 | case XML_ATTRIBUTE_DECL: |
| 3454 | case XML_ENTITY_DECL: |
| 3455 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | /** |
| 3460 | * xmlNodeAddContent: |
| 3461 | * @cur: the node being modified |
| 3462 | * @content: extra content |
| 3463 | * |
| 3464 | * Append the extra substring to the node content. |
| 3465 | */ |
| 3466 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3467 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3468 | int len; |
| 3469 | |
| 3470 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3471 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3472 | xmlGenericError(xmlGenericErrorContext, |
| 3473 | "xmlNodeAddContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3474 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3475 | return; |
| 3476 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3477 | if (content == NULL) return; |
| 3478 | len = xmlStrlen(content); |
| 3479 | xmlNodeAddContentLen(cur, content, len); |
| 3480 | } |
| 3481 | |
| 3482 | /** |
| 3483 | * xmlTextMerge: |
| 3484 | * @first: the first text node |
| 3485 | * @second: the second text node being merged |
| 3486 | * |
| 3487 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3488 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3489 | */ |
| 3490 | xmlNodePtr |
| 3491 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 3492 | if (first == NULL) return(second); |
| 3493 | if (second == NULL) return(first); |
| 3494 | if (first->type != XML_TEXT_NODE) return(first); |
| 3495 | if (second->type != XML_TEXT_NODE) return(first); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3496 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3497 | xmlNodeAddContent(first, second->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3498 | #else |
| 3499 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 3500 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3501 | xmlUnlinkNode(second); |
| 3502 | xmlFreeNode(second); |
| 3503 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3506 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3507 | * xmlGetNsList: |
| 3508 | * @doc: the document |
| 3509 | * @node: the current node |
| 3510 | * |
| 3511 | * Search all the namespace applying to a given element. |
| 3512 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 3513 | * that need to be freed by the caller or NULL if no |
| 3514 | * namespace if defined |
| 3515 | */ |
| 3516 | xmlNsPtr * |
| 3517 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 3518 | xmlNsPtr cur; |
| 3519 | xmlNsPtr *ret = NULL; |
| 3520 | int nbns = 0; |
| 3521 | int maxns = 10; |
| 3522 | int i; |
| 3523 | |
| 3524 | while (node != NULL) { |
| 3525 | cur = node->nsDef; |
| 3526 | while (cur != NULL) { |
| 3527 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3528 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3529 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3530 | xmlGenericError(xmlGenericErrorContext, |
| 3531 | "xmlGetNsList : out of memory!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3532 | return(NULL); |
| 3533 | } |
| 3534 | ret[nbns] = NULL; |
| 3535 | } |
| 3536 | for (i = 0;i < nbns;i++) { |
| 3537 | if ((cur->prefix == ret[i]->prefix) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3538 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3539 | } |
| 3540 | if (i >= nbns) { |
| 3541 | if (nbns >= maxns) { |
| 3542 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3543 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3544 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 3545 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3546 | xmlGenericError(xmlGenericErrorContext, |
| 3547 | "xmlGetNsList : realloc failed!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3548 | return(NULL); |
| 3549 | } |
| 3550 | } |
| 3551 | ret[nbns++] = cur; |
| 3552 | ret[nbns] = NULL; |
| 3553 | } |
| 3554 | |
| 3555 | cur = cur->next; |
| 3556 | } |
| 3557 | node = node->parent; |
| 3558 | } |
| 3559 | return(ret); |
| 3560 | } |
| 3561 | |
| 3562 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3563 | * xmlSearchNs: |
| 3564 | * @doc: the document |
| 3565 | * @node: the current node |
| 3566 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3567 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3568 | * Search a Ns registered under a given name space for a document. |
| 3569 | * recurse on the parents until it finds the defined namespace |
| 3570 | * or return NULL otherwise. |
| 3571 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3572 | * We don't allow to cross entities boundaries. If you don't declare |
| 3573 | * the namespace within those you will be in troubles !!! A warning |
| 3574 | * is generated to cover this case. |
| 3575 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3576 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3577 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3578 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3579 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3580 | xmlNsPtr cur; |
| 3581 | |
Daniel Veillard | 62ba71e | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 3582 | if (node == NULL) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3583 | while (node != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3584 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 3585 | (node->type == XML_ENTITY_NODE) || |
| 3586 | (node->type == XML_ENTITY_DECL)) |
| 3587 | return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3588 | if (node->type == XML_ELEMENT_NODE) { |
| 3589 | cur = node->nsDef; |
| 3590 | while (cur != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3591 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 3592 | (cur->href != NULL)) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3593 | return(cur); |
| 3594 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3595 | (cur->href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3596 | (xmlStrEqual(cur->prefix, nameSpace))) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3597 | return(cur); |
| 3598 | cur = cur->next; |
| 3599 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3600 | } |
| 3601 | node = node->parent; |
| 3602 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3603 | return(NULL); |
| 3604 | } |
| 3605 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3606 | /** |
| 3607 | * xmlSearchNsByHref: |
| 3608 | * @doc: the document |
| 3609 | * @node: the current node |
| 3610 | * @href: the namespace value |
| 3611 | * |
| 3612 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 3613 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3614 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3615 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3616 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3617 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3618 | xmlNsPtr cur; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3619 | xmlNodePtr orig = node; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3620 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3621 | if ((node == NULL) || (href == NULL)) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3622 | while (node != NULL) { |
| 3623 | cur = node->nsDef; |
| 3624 | while (cur != NULL) { |
| 3625 | if ((cur->href != NULL) && (href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3626 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3627 | /* |
| 3628 | * Check that the prefix is not shadowed between orig and node |
| 3629 | */ |
| 3630 | xmlNodePtr check = orig; |
| 3631 | xmlNsPtr tst; |
| 3632 | |
| 3633 | while (check != node) { |
| 3634 | tst = check->nsDef; |
| 3635 | while (tst != NULL) { |
| 3636 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 3637 | goto shadowed; |
| 3638 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3639 | (xmlStrEqual(tst->prefix, cur->prefix))) |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3640 | goto shadowed; |
| 3641 | tst = tst->next; |
| 3642 | } |
Daniel Veillard | c4f4f0b | 2000-10-29 17:46:30 +0000 | [diff] [blame] | 3643 | check = check->parent; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3644 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3645 | return(cur); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3646 | } |
| 3647 | shadowed: |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3648 | cur = cur->next; |
| 3649 | } |
| 3650 | node = node->parent; |
| 3651 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3652 | return(NULL); |
| 3653 | } |
| 3654 | |
| 3655 | /** |
| 3656 | * xmlNewReconciliedNs |
| 3657 | * @doc: the document |
| 3658 | * @tree: a node expected to hold the new namespace |
| 3659 | * @ns: the original namespace |
| 3660 | * |
| 3661 | * This function tries to locate a namespace definition in a tree |
| 3662 | * ancestors, or create a new namespace definition node similar to |
| 3663 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 3664 | * null (default namespace) or reused within the subtree defined by |
| 3665 | * @tree or on one of its ancestors then a new prefix is generated. |
| 3666 | * Returns the (new) namespace definition or NULL in case of error |
| 3667 | */ |
| 3668 | xmlNsPtr |
| 3669 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 3670 | xmlNsPtr def; |
| 3671 | xmlChar prefix[50]; |
| 3672 | int counter = 1; |
| 3673 | |
| 3674 | if (tree == NULL) { |
| 3675 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3676 | xmlGenericError(xmlGenericErrorContext, |
| 3677 | "xmlNewReconciliedNs : tree == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3678 | #endif |
| 3679 | return(NULL); |
| 3680 | } |
| 3681 | if (ns == NULL) { |
| 3682 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3683 | xmlGenericError(xmlGenericErrorContext, |
| 3684 | "xmlNewReconciliedNs : ns == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3685 | #endif |
| 3686 | return(NULL); |
| 3687 | } |
| 3688 | /* |
| 3689 | * Search an existing namespace definition inherited. |
| 3690 | */ |
| 3691 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 3692 | if (def != NULL) |
| 3693 | return(def); |
| 3694 | |
| 3695 | /* |
| 3696 | * Find a close prefix which is not already in use. |
| 3697 | * Let's strip namespace prefixes longer than 20 chars ! |
| 3698 | */ |
| 3699 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 3700 | def = xmlSearchNs(doc, tree, prefix); |
| 3701 | while (def != NULL) { |
| 3702 | if (counter > 1000) return(NULL); |
| 3703 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 3704 | def = xmlSearchNs(doc, tree, prefix); |
| 3705 | } |
| 3706 | |
| 3707 | /* |
| 3708 | * Ok, now we are ready to create a new one. |
| 3709 | */ |
| 3710 | def = xmlNewNs(tree, ns->href, prefix); |
| 3711 | return(def); |
| 3712 | } |
| 3713 | |
| 3714 | /** |
| 3715 | * xmlReconciliateNs |
| 3716 | * @doc: the document |
| 3717 | * @tree: a node defining the subtree to reconciliate |
| 3718 | * |
| 3719 | * This function checks that all the namespaces declared within the given |
| 3720 | * tree are properly declared. This is needed for example after Copy or Cut |
| 3721 | * and then paste operations. The subtree may still hold pointers to |
| 3722 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3723 | * as possible the function try tu reuse the existing namespaces found in |
| 3724 | * the new environment. If not possible the new namespaces are redeclared |
| 3725 | * on @tree at the top of the given subtree. |
| 3726 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3727 | */ |
| 3728 | int |
| 3729 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3730 | xmlNsPtr *oldNs = NULL; |
| 3731 | xmlNsPtr *newNs = NULL; |
| 3732 | int sizeCache = 0; |
| 3733 | int nbCache = 0; |
| 3734 | |
| 3735 | xmlNsPtr n; |
| 3736 | xmlNodePtr node = tree; |
| 3737 | xmlAttrPtr attr; |
| 3738 | int ret = 0, i; |
| 3739 | |
| 3740 | while (node != NULL) { |
| 3741 | /* |
| 3742 | * Reconciliate the node namespace |
| 3743 | */ |
| 3744 | if (node->ns != NULL) { |
| 3745 | /* |
| 3746 | * initialize the cache if needed |
| 3747 | */ |
| 3748 | if (sizeCache == 0) { |
| 3749 | sizeCache = 10; |
| 3750 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3751 | sizeof(xmlNsPtr)); |
| 3752 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3753 | xmlGenericError(xmlGenericErrorContext, |
| 3754 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3755 | return(-1); |
| 3756 | } |
| 3757 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3758 | sizeof(xmlNsPtr)); |
| 3759 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3760 | xmlGenericError(xmlGenericErrorContext, |
| 3761 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3762 | xmlFree(oldNs); |
| 3763 | return(-1); |
| 3764 | } |
| 3765 | } |
| 3766 | for (i = 0;i < nbCache;i++) { |
| 3767 | if (oldNs[i] == node->ns) { |
| 3768 | node->ns = newNs[i]; |
| 3769 | break; |
| 3770 | } |
| 3771 | } |
| 3772 | if (i == nbCache) { |
| 3773 | /* |
| 3774 | * Ok we need to recreate a new namespace definition |
| 3775 | */ |
| 3776 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3777 | if (n != NULL) { /* :-( what if else ??? */ |
| 3778 | /* |
| 3779 | * check if we need to grow the cache buffers. |
| 3780 | */ |
| 3781 | if (sizeCache <= nbCache) { |
| 3782 | sizeCache *= 2; |
| 3783 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3784 | sizeof(xmlNsPtr)); |
| 3785 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3786 | xmlGenericError(xmlGenericErrorContext, |
| 3787 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3788 | xmlFree(newNs); |
| 3789 | return(-1); |
| 3790 | } |
| 3791 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3792 | sizeof(xmlNsPtr)); |
| 3793 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3794 | xmlGenericError(xmlGenericErrorContext, |
| 3795 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3796 | xmlFree(oldNs); |
| 3797 | return(-1); |
| 3798 | } |
| 3799 | } |
| 3800 | newNs[nbCache] = n; |
| 3801 | oldNs[nbCache++] = node->ns; |
| 3802 | node->ns = n; |
| 3803 | } |
| 3804 | } |
| 3805 | } |
| 3806 | /* |
| 3807 | * now check for namespace hold by attributes on the node. |
| 3808 | */ |
| 3809 | attr = node->properties; |
| 3810 | while (attr != NULL) { |
| 3811 | if (attr->ns != NULL) { |
| 3812 | /* |
| 3813 | * initialize the cache if needed |
| 3814 | */ |
| 3815 | if (sizeCache == 0) { |
| 3816 | sizeCache = 10; |
| 3817 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3818 | sizeof(xmlNsPtr)); |
| 3819 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3820 | xmlGenericError(xmlGenericErrorContext, |
| 3821 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3822 | return(-1); |
| 3823 | } |
| 3824 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3825 | sizeof(xmlNsPtr)); |
| 3826 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3827 | xmlGenericError(xmlGenericErrorContext, |
| 3828 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3829 | xmlFree(oldNs); |
| 3830 | return(-1); |
| 3831 | } |
| 3832 | } |
| 3833 | for (i = 0;i < nbCache;i++) { |
| 3834 | if (oldNs[i] == attr->ns) { |
| 3835 | node->ns = newNs[i]; |
| 3836 | break; |
| 3837 | } |
| 3838 | } |
| 3839 | if (i == nbCache) { |
| 3840 | /* |
| 3841 | * Ok we need to recreate a new namespace definition |
| 3842 | */ |
| 3843 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3844 | if (n != NULL) { /* :-( what if else ??? */ |
| 3845 | /* |
| 3846 | * check if we need to grow the cache buffers. |
| 3847 | */ |
| 3848 | if (sizeCache <= nbCache) { |
| 3849 | sizeCache *= 2; |
| 3850 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3851 | sizeof(xmlNsPtr)); |
| 3852 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3853 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3854 | "xmlReconciliateNs : memory pbm\n"); |
| 3855 | xmlFree(newNs); |
| 3856 | return(-1); |
| 3857 | } |
| 3858 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3859 | sizeof(xmlNsPtr)); |
| 3860 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3861 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3862 | "xmlReconciliateNs : memory pbm\n"); |
| 3863 | xmlFree(oldNs); |
| 3864 | return(-1); |
| 3865 | } |
| 3866 | } |
| 3867 | newNs[nbCache] = n; |
| 3868 | oldNs[nbCache++] = attr->ns; |
| 3869 | attr->ns = n; |
| 3870 | } |
| 3871 | } |
| 3872 | } |
| 3873 | attr = attr->next; |
| 3874 | } |
| 3875 | |
| 3876 | /* |
| 3877 | * Browse the full subtree, deep first |
| 3878 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3879 | if (node->children != NULL) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3880 | /* deep first */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3881 | node = node->children; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3882 | } else if ((node != tree) && (node->next != NULL)) { |
| 3883 | /* then siblings */ |
| 3884 | node = node->next; |
| 3885 | } else if (node != tree) { |
| 3886 | /* go up to parents->next if needed */ |
| 3887 | while (node != tree) { |
| 3888 | if (node->parent != NULL) |
| 3889 | node = node->parent; |
| 3890 | if ((node != tree) && (node->next != NULL)) { |
| 3891 | node = node->next; |
| 3892 | break; |
| 3893 | } |
| 3894 | if (node->parent == NULL) { |
| 3895 | node = NULL; |
| 3896 | break; |
| 3897 | } |
| 3898 | } |
| 3899 | /* exit condition */ |
| 3900 | if (node == tree) |
| 3901 | node = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3904 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3907 | /** |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3908 | * xmlHasProp: |
| 3909 | * @node: the node |
| 3910 | * @name: the attribute name |
| 3911 | * |
| 3912 | * Search an attribute associated to a node |
| 3913 | * This function also looks in DTD attribute declaration for #FIXED or |
| 3914 | * default declaration values unless DTD use has been turned off. |
| 3915 | * |
| 3916 | * Returns the attribute or the attribute declaration or NULL if |
| 3917 | * neither was found. |
| 3918 | */ |
| 3919 | xmlAttrPtr |
| 3920 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 3921 | xmlAttrPtr prop; |
| 3922 | xmlDocPtr doc; |
| 3923 | |
| 3924 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3925 | /* |
| 3926 | * Check on the properties attached to the node |
| 3927 | */ |
| 3928 | prop = node->properties; |
| 3929 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3930 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3931 | return(prop); |
| 3932 | } |
| 3933 | prop = prop->next; |
| 3934 | } |
| 3935 | if (!xmlCheckDTD) return(NULL); |
| 3936 | |
| 3937 | /* |
| 3938 | * Check if there is a default declaration in the internal |
| 3939 | * or external subsets |
| 3940 | */ |
| 3941 | doc = node->doc; |
| 3942 | if (doc != NULL) { |
| 3943 | xmlAttributePtr attrDecl; |
| 3944 | if (doc->intSubset != NULL) { |
| 3945 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3946 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3947 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3948 | if (attrDecl != NULL) |
| 3949 | return((xmlAttrPtr) attrDecl); |
| 3950 | } |
| 3951 | } |
| 3952 | return(NULL); |
| 3953 | } |
| 3954 | |
| 3955 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3956 | * xmlGetProp: |
| 3957 | * @node: the node |
| 3958 | * @name: the attribute name |
| 3959 | * |
| 3960 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3961 | * This does the entity substitution. |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3962 | * This function looks in DTD attribute declaration for #FIXED or |
| 3963 | * default declaration values unless DTD use has been turned off. |
| 3964 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3965 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3966 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3967 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3968 | xmlChar * |
| 3969 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3970 | xmlAttrPtr prop; |
| 3971 | xmlDocPtr doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3972 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3973 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3974 | /* |
| 3975 | * Check on the properties attached to the node |
| 3976 | */ |
| 3977 | prop = node->properties; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3978 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3979 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3980 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3981 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3982 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3983 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3984 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3985 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3986 | prop = prop->next; |
| 3987 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3988 | if (!xmlCheckDTD) return(NULL); |
| 3989 | |
| 3990 | /* |
| 3991 | * Check if there is a default declaration in the internal |
| 3992 | * or external subsets |
| 3993 | */ |
| 3994 | doc = node->doc; |
| 3995 | if (doc != NULL) { |
| 3996 | xmlAttributePtr attrDecl; |
| 3997 | if (doc->intSubset != NULL) { |
| 3998 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3999 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4000 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | f967b90 | 2000-01-17 16:06:10 +0000 | [diff] [blame] | 4001 | if (attrDecl != NULL) |
| 4002 | return(xmlStrdup(attrDecl->defaultValue)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | return(NULL); |
| 4006 | } |
| 4007 | |
| 4008 | /** |
| 4009 | * xmlGetNsProp: |
| 4010 | * @node: the node |
| 4011 | * @name: the attribute name |
| 4012 | * @namespace: the URI of the namespace |
| 4013 | * |
| 4014 | * Search and get the value of an attribute associated to a node |
| 4015 | * This attribute has to be anchored in the namespace specified. |
| 4016 | * This does the entity substitution. |
| 4017 | * This function looks in DTD attribute declaration for #FIXED or |
| 4018 | * default declaration values unless DTD use has been turned off. |
| 4019 | * |
| 4020 | * Returns the attribute value or NULL if not found. |
| 4021 | * It's up to the caller to free the memory. |
| 4022 | */ |
| 4023 | xmlChar * |
| 4024 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
| 4025 | xmlAttrPtr prop = node->properties; |
| 4026 | xmlDocPtr doc; |
| 4027 | xmlNsPtr ns; |
| 4028 | |
| 4029 | if (namespace == NULL) |
| 4030 | return(xmlGetProp(node, name)); |
| 4031 | while (prop != NULL) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4032 | /* |
| 4033 | * One need to have |
| 4034 | * - same attribute names |
| 4035 | * - and the attribute carrying that namespace |
| 4036 | * or |
| 4037 | * no namespace on the attribute and the element carrying it |
| 4038 | */ |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4039 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4040 | (((prop->ns == NULL) && (node->ns != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4041 | (xmlStrEqual(node->ns->href, namespace))) || |
| 4042 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, namespace))))) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4043 | xmlChar *ret; |
| 4044 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4045 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4046 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 4047 | return(ret); |
| 4048 | } |
| 4049 | prop = prop->next; |
| 4050 | } |
| 4051 | if (!xmlCheckDTD) return(NULL); |
| 4052 | |
| 4053 | /* |
| 4054 | * Check if there is a default declaration in the internal |
| 4055 | * or external subsets |
| 4056 | */ |
| 4057 | doc = node->doc; |
| 4058 | if (doc != NULL) { |
| 4059 | xmlAttributePtr attrDecl; |
| 4060 | if (doc->intSubset != NULL) { |
| 4061 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4062 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4063 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4064 | |
| 4065 | if (attrDecl->prefix != NULL) { |
| 4066 | /* |
| 4067 | * The DTD declaration only allows a prefix search |
| 4068 | */ |
| 4069 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4070 | if ((ns != NULL) && (xmlStrEqual(ns->href, namespace))) |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4071 | return(xmlStrdup(attrDecl->defaultValue)); |
| 4072 | } |
| 4073 | } |
| 4074 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4075 | return(NULL); |
| 4076 | } |
| 4077 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4078 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4079 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4080 | * @node: the node |
| 4081 | * @name: the attribute name |
| 4082 | * @value: the attribute value |
| 4083 | * |
| 4084 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4085 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4086 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4087 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4088 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4089 | xmlAttrPtr prop = node->properties; |
| 4090 | |
| 4091 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4092 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4093 | if (prop->children != NULL) |
| 4094 | xmlFreeNodeList(prop->children); |
| 4095 | prop->children = NULL; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4096 | prop->last = NULL; |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4097 | if (value != NULL) { |
| 4098 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4099 | xmlNodePtr tmp; |
| 4100 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4101 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4102 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4103 | prop->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4104 | tmp = prop->children; |
| 4105 | while (tmp != NULL) { |
| 4106 | tmp->parent = (xmlNodePtr) prop; |
| 4107 | if (tmp->next == NULL) |
| 4108 | prop->last = tmp; |
| 4109 | tmp = tmp->next; |
| 4110 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4111 | xmlFree(buffer); |
| 4112 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4113 | return(prop); |
| 4114 | } |
| 4115 | prop = prop->next; |
| 4116 | } |
| 4117 | prop = xmlNewProp(node, name, value); |
| 4118 | return(prop); |
| 4119 | } |
| 4120 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4121 | /** |
| 4122 | * xmlNodeIsText: |
| 4123 | * @node: the node |
| 4124 | * |
| 4125 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4126 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4127 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4128 | int |
| 4129 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4130 | if (node == NULL) return(0); |
| 4131 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4132 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4133 | return(0); |
| 4134 | } |
| 4135 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4136 | /** |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4137 | * xmlIsBlankNode: |
| 4138 | * @node: the node |
| 4139 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4140 | * Checks whether this node is an empty or whitespace only |
| 4141 | * (and possibly ignorable) text-node. |
| 4142 | * |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4143 | * Returns 1 yes, 0 no |
| 4144 | */ |
| 4145 | int |
| 4146 | xmlIsBlankNode(xmlNodePtr node) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4147 | const xmlChar *cur; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4148 | if (node == NULL) return(0); |
| 4149 | |
| 4150 | if (node->type != XML_TEXT_NODE) return(0); |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 4151 | if (node->content == NULL) return(1); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4152 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4153 | cur = node->content; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4154 | #else |
| 4155 | cur = xmlBufferContent(node->content); |
| 4156 | #endif |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4157 | while (*cur != 0) { |
| 4158 | if (!IS_BLANK(*cur)) return(0); |
| 4159 | cur++; |
| 4160 | } |
| 4161 | |
| 4162 | return(1); |
| 4163 | } |
| 4164 | |
| 4165 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4166 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4167 | * @node: the node |
| 4168 | * @content: the content |
| 4169 | * @len: @content lenght |
| 4170 | * |
| 4171 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4172 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4173 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4174 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4175 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4176 | if (node == NULL) return; |
| 4177 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4178 | if ((node->type != XML_TEXT_NODE) && |
| 4179 | (node->type != XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4180 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4181 | xmlGenericError(xmlGenericErrorContext, |
| 4182 | "xmlTextConcat: node is not text nor cdata\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4183 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4184 | return; |
| 4185 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4186 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4187 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4188 | #else |
| 4189 | xmlBufferAdd(node->content, content, len); |
| 4190 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | /************************************************************************ |
| 4194 | * * |
| 4195 | * Output : to a FILE or in memory * |
| 4196 | * * |
| 4197 | ************************************************************************/ |
| 4198 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4199 | #define BASE_BUFFER_SIZE 4000 |
| 4200 | |
| 4201 | /** |
| 4202 | * xmlBufferCreate: |
| 4203 | * |
| 4204 | * routine to create an XML buffer. |
| 4205 | * returns the new structure. |
| 4206 | */ |
| 4207 | xmlBufferPtr |
| 4208 | xmlBufferCreate(void) { |
| 4209 | xmlBufferPtr ret; |
| 4210 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4211 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4212 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4213 | xmlGenericError(xmlGenericErrorContext, |
| 4214 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4215 | return(NULL); |
| 4216 | } |
| 4217 | ret->use = 0; |
| 4218 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4219 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4220 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4221 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4222 | xmlGenericError(xmlGenericErrorContext, |
| 4223 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4224 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4225 | return(NULL); |
| 4226 | } |
| 4227 | ret->content[0] = 0; |
| 4228 | return(ret); |
| 4229 | } |
| 4230 | |
| 4231 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4232 | * xmlBufferCreateSize: |
| 4233 | * @size: initial size of buffer |
| 4234 | * |
| 4235 | * routine to create an XML buffer. |
| 4236 | * returns the new structure. |
| 4237 | */ |
| 4238 | xmlBufferPtr |
| 4239 | xmlBufferCreateSize(size_t size) { |
| 4240 | xmlBufferPtr ret; |
| 4241 | |
| 4242 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 4243 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4244 | xmlGenericError(xmlGenericErrorContext, |
| 4245 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4246 | return(NULL); |
| 4247 | } |
| 4248 | ret->use = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4249 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4250 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4251 | if (ret->size){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4252 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 4253 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4254 | xmlGenericError(xmlGenericErrorContext, |
| 4255 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4256 | xmlFree(ret); |
| 4257 | return(NULL); |
| 4258 | } |
| 4259 | ret->content[0] = 0; |
| 4260 | } else |
| 4261 | ret->content = NULL; |
| 4262 | return(ret); |
| 4263 | } |
| 4264 | |
| 4265 | /** |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 4266 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4267 | * @buf: the buffer to free |
| 4268 | * @scheme: allocation scheme to use |
| 4269 | * |
| 4270 | * Sets the allocation scheme for this buffer |
| 4271 | */ |
| 4272 | void |
| 4273 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 4274 | xmlBufferAllocationScheme scheme) { |
| 4275 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4276 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4277 | xmlGenericError(xmlGenericErrorContext, |
| 4278 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4279 | #endif |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4280 | return; |
| 4281 | } |
| 4282 | |
| 4283 | buf->alloc = scheme; |
| 4284 | } |
| 4285 | |
| 4286 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4287 | * xmlBufferFree: |
| 4288 | * @buf: the buffer to free |
| 4289 | * |
| 4290 | * Frees an XML buffer. |
| 4291 | */ |
| 4292 | void |
| 4293 | xmlBufferFree(xmlBufferPtr buf) { |
| 4294 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4295 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4296 | xmlGenericError(xmlGenericErrorContext, |
| 4297 | "xmlBufferFree: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4298 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4299 | return; |
| 4300 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4301 | if (buf->content != NULL) { |
| 4302 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4303 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4304 | #else |
| 4305 | memset(buf->content, -1, buf->size); |
| 4306 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4307 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4308 | } |
| 4309 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4310 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4314 | * xmlBufferEmpty: |
| 4315 | * @buf: the buffer |
| 4316 | * |
| 4317 | * empty a buffer. |
| 4318 | */ |
| 4319 | void |
| 4320 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | 4fb87ee | 2000-09-19 12:25:59 +0000 | [diff] [blame] | 4321 | if (buf->content == NULL) return; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4322 | buf->use = 0; |
| 4323 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 4324 | } |
| 4325 | |
| 4326 | /** |
| 4327 | * xmlBufferShrink: |
| 4328 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4329 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4330 | * |
| 4331 | * Remove the beginning of an XML buffer. |
| 4332 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4333 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4334 | */ |
| 4335 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4336 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4337 | if (len == 0) return(0); |
| 4338 | if (len > buf->use) return(-1); |
| 4339 | |
| 4340 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4341 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4342 | |
| 4343 | buf->content[buf->use] = 0; |
| 4344 | return(len); |
| 4345 | } |
| 4346 | |
| 4347 | /** |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4348 | * xmlBufferGrow: |
| 4349 | * @buf: the buffer |
| 4350 | * @len: the minimum free sie to allocate |
| 4351 | * |
| 4352 | * Grow the available space of an XML buffer. |
| 4353 | * |
| 4354 | * Returns the new available space or -1 in case of error |
| 4355 | */ |
| 4356 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4357 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4358 | int size; |
| 4359 | xmlChar *newbuf; |
| 4360 | |
| 4361 | if (len <= buf->use) return(0); |
| 4362 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4363 | size = buf->use + len + 100; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4364 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4365 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4366 | if (newbuf == NULL) return(-1); |
| 4367 | buf->content = newbuf; |
| 4368 | buf->size = size; |
| 4369 | return(buf->size - buf->use); |
| 4370 | } |
| 4371 | |
| 4372 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4373 | * xmlBufferDump: |
| 4374 | * @file: the file output |
| 4375 | * @buf: the buffer to dump |
| 4376 | * |
| 4377 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4378 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4379 | */ |
| 4380 | int |
| 4381 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 4382 | int ret; |
| 4383 | |
| 4384 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4385 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4386 | xmlGenericError(xmlGenericErrorContext, |
| 4387 | "xmlBufferDump: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4388 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4389 | return(0); |
| 4390 | } |
| 4391 | if (buf->content == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4392 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4393 | xmlGenericError(xmlGenericErrorContext, |
| 4394 | "xmlBufferDump: buf->content == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4395 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4396 | return(0); |
| 4397 | } |
| 4398 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4399 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4400 | return(ret); |
| 4401 | } |
| 4402 | |
| 4403 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4404 | * xmlBufferContent: |
| 4405 | * @buf: the buffer to resize |
| 4406 | * |
| 4407 | * Returns the internal content |
| 4408 | */ |
| 4409 | |
| 4410 | const xmlChar* |
| 4411 | xmlBufferContent(const xmlBufferPtr buf) |
| 4412 | { |
| 4413 | if(!buf) |
| 4414 | return NULL; |
| 4415 | |
| 4416 | return buf->content; |
| 4417 | } |
| 4418 | |
| 4419 | /** |
| 4420 | * xmlBufferLength: |
| 4421 | * @buf: the buffer |
| 4422 | * |
| 4423 | * Returns the length of data in the internal content |
| 4424 | */ |
| 4425 | |
| 4426 | int |
| 4427 | xmlBufferLength(const xmlBufferPtr buf) |
| 4428 | { |
| 4429 | if(!buf) |
| 4430 | return 0; |
| 4431 | |
| 4432 | return buf->use; |
| 4433 | } |
| 4434 | |
| 4435 | /** |
| 4436 | * xmlBufferResize: |
| 4437 | * @buf: the buffer to resize |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4438 | * @size: the desired size |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4439 | * |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4440 | * Resize a buffer to accomodate minimum size of @size. |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4441 | * |
| 4442 | * Returns 0 in case of problems, 1 otherwise |
| 4443 | */ |
| 4444 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4445 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4446 | { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4447 | unsigned int newSize; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4448 | xmlChar* rebuf = NULL; |
| 4449 | |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4450 | /*take care of empty case*/ |
| 4451 | newSize = (buf->size ? buf->size*2 : size); |
| 4452 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4453 | /* Don't resize if we don't have to */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4454 | if (size < buf->size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4455 | return 1; |
| 4456 | |
| 4457 | /* figure out new size */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4458 | switch (buf->alloc){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4459 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4460 | while (size > newSize) newSize *= 2; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4461 | break; |
| 4462 | case XML_BUFFER_ALLOC_EXACT: |
| 4463 | newSize = size+10; |
| 4464 | break; |
| 4465 | default: |
| 4466 | newSize = size+10; |
| 4467 | break; |
| 4468 | } |
| 4469 | |
| 4470 | if (buf->content == NULL) |
| 4471 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 4472 | else |
| 4473 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 4474 | newSize * sizeof(xmlChar)); |
| 4475 | if (rebuf == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4476 | xmlGenericError(xmlGenericErrorContext, |
| 4477 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4478 | return 0; |
| 4479 | } |
| 4480 | buf->content = rebuf; |
| 4481 | buf->size = newSize; |
| 4482 | |
| 4483 | return 1; |
| 4484 | } |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4485 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4486 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4487 | * xmlBufferAdd: |
| 4488 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4489 | * @str: the xmlChar string |
| 4490 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4491 | * |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4492 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 4493 | * str is recomputed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4494 | */ |
| 4495 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4496 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4497 | unsigned int needSize; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4498 | |
| 4499 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4500 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4501 | xmlGenericError(xmlGenericErrorContext, |
| 4502 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4503 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4504 | return; |
| 4505 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4506 | if (len < -1) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4507 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4508 | xmlGenericError(xmlGenericErrorContext, |
| 4509 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4510 | #endif |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4511 | return; |
| 4512 | } |
| 4513 | if (len == 0) return; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4514 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4515 | if (len < 0) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4516 | len = xmlStrlen(str); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4517 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4518 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4519 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4520 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4521 | if (needSize > buf->size){ |
| 4522 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4523 | xmlGenericError(xmlGenericErrorContext, |
| 4524 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4525 | return; |
| 4526 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4527 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4528 | |
| 4529 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4530 | buf->use += len; |
| 4531 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4535 | * xmlBufferAddHead: |
| 4536 | * @buf: the buffer |
| 4537 | * @str: the xmlChar string |
| 4538 | * @len: the number of xmlChar to add |
| 4539 | * |
| 4540 | * Add a string range to the beginning of an XML buffer. |
| 4541 | * if len == -1, the lenght of @str is recomputed. |
| 4542 | */ |
| 4543 | void |
| 4544 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4545 | unsigned int needSize; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4546 | |
| 4547 | if (str == NULL) { |
| 4548 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4549 | xmlGenericError(xmlGenericErrorContext, |
| 4550 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4551 | #endif |
| 4552 | return; |
| 4553 | } |
| 4554 | if (len < -1) { |
| 4555 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4556 | xmlGenericError(xmlGenericErrorContext, |
| 4557 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4558 | #endif |
| 4559 | return; |
| 4560 | } |
| 4561 | if (len == 0) return; |
| 4562 | |
| 4563 | if (len < 0) |
| 4564 | len = xmlStrlen(str); |
| 4565 | |
| 4566 | if (len <= 0) return; |
| 4567 | |
| 4568 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4569 | if (needSize > buf->size){ |
| 4570 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4571 | xmlGenericError(xmlGenericErrorContext, |
| 4572 | "xmlBufferAddHead : out of memory!\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4573 | return; |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 4578 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 4579 | buf->use += len; |
| 4580 | buf->content[buf->use] = 0; |
| 4581 | } |
| 4582 | |
| 4583 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4584 | * xmlBufferCat: |
| 4585 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4586 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4587 | * |
| 4588 | * Append a zero terminated string to an XML buffer. |
| 4589 | */ |
| 4590 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4591 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4592 | if (str != NULL) |
| 4593 | xmlBufferAdd(buf, str, -1); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | /** |
| 4597 | * xmlBufferCCat: |
| 4598 | * @buf: the buffer to dump |
| 4599 | * @str: the C char string |
| 4600 | * |
| 4601 | * Append a zero terminated C string to an XML buffer. |
| 4602 | */ |
| 4603 | void |
| 4604 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 4605 | const char *cur; |
| 4606 | |
| 4607 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4608 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4609 | xmlGenericError(xmlGenericErrorContext, |
| 4610 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4611 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4612 | return; |
| 4613 | } |
| 4614 | for (cur = str;*cur != 0;cur++) { |
| 4615 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4616 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4617 | xmlGenericError(xmlGenericErrorContext, |
| 4618 | "xmlBufferCCat : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4619 | return; |
| 4620 | } |
| 4621 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4622 | buf->content[buf->use++] = *cur; |
| 4623 | } |
| 4624 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4625 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4626 | /** |
| 4627 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4628 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4629 | * @string: the string to add |
| 4630 | * |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4631 | * routine which manages and grows an output buffer. This one adds |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4632 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4633 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4634 | void |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4635 | #ifdef VMS |
| 4636 | xmlBufferWriteXmlCHAR |
| 4637 | #else |
| 4638 | xmlBufferWriteCHAR |
| 4639 | #endif |
| 4640 | (xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4641 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4644 | /** |
| 4645 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4646 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4647 | * @string: the string to add |
| 4648 | * |
| 4649 | * routine which manage and grows an output buffer. This one add |
| 4650 | * C chars at the end of the array. |
| 4651 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4652 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4653 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 4654 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4657 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4658 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4659 | * xmlBufferWriteQuotedString: |
| 4660 | * @buf: the XML buffer output |
| 4661 | * @string: the string to add |
| 4662 | * |
| 4663 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4664 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4665 | * quote or double-quotes internally |
| 4666 | */ |
| 4667 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4668 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4669 | if (xmlStrchr(string, '"')) { |
| 4670 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4671 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4672 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4673 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4674 | #endif |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4675 | } |
| 4676 | xmlBufferCCat(buf, "'"); |
| 4677 | xmlBufferCat(buf, string); |
| 4678 | xmlBufferCCat(buf, "'"); |
| 4679 | } else { |
| 4680 | xmlBufferCCat(buf, "\""); |
| 4681 | xmlBufferCat(buf, string); |
| 4682 | xmlBufferCCat(buf, "\""); |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4687 | /************************************************************************ |
| 4688 | * * |
| 4689 | * Dumping XML tree content to a simple buffer * |
| 4690 | * * |
| 4691 | ************************************************************************/ |
| 4692 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4693 | void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4694 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4695 | int format); |
| 4696 | static void |
| 4697 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4698 | int format); |
| 4699 | void |
| 4700 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 4701 | |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4702 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4703 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4704 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4705 | * @cur: a namespace |
| 4706 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4707 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4708 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4709 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4710 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4711 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4712 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4713 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4714 | xmlGenericError(xmlGenericErrorContext, |
| 4715 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4716 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4717 | return; |
| 4718 | } |
| 4719 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4720 | /* Within the context of an element attributes */ |
| 4721 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4722 | xmlBufferWriteChar(buf, " xmlns:"); |
| 4723 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4724 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4725 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4726 | xmlBufferWriteChar(buf, "="); |
| 4727 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4728 | } |
| 4729 | } |
| 4730 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4731 | /** |
| 4732 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4733 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4734 | * @cur: the first namespace |
| 4735 | * |
| 4736 | * Dump a list of local Namespace definitions. |
| 4737 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4738 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4739 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4740 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4741 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4742 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4743 | cur = cur->next; |
| 4744 | } |
| 4745 | } |
| 4746 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4747 | /** |
| 4748 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4749 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4750 | * @doc: the document |
| 4751 | * |
| 4752 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4753 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4754 | static void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4755 | xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) { |
| 4756 | if (dtd == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4757 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4758 | xmlGenericError(xmlGenericErrorContext, |
| 4759 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4760 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4761 | return; |
| 4762 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4763 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4764 | xmlBufferWriteCHAR(buf, dtd->name); |
| 4765 | if (dtd->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4766 | xmlBufferWriteChar(buf, " PUBLIC "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4767 | xmlBufferWriteQuotedString(buf, dtd->ExternalID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4768 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4769 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4770 | } else if (dtd->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4771 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4772 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4773 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4774 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4775 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4776 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4777 | return; |
| 4778 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4779 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4780 | xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0); |
| 4781 | #if 0 |
| 4782 | if (dtd->entities != NULL) |
| 4783 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4784 | if (dtd->notations != NULL) |
| 4785 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4786 | if (dtd->elements != NULL) |
| 4787 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 4788 | if (dtd->attributes != NULL) |
| 4789 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 4790 | #endif |
| 4791 | xmlBufferWriteChar(buf, "]>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4794 | /** |
| 4795 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4796 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4797 | * @doc: the document |
| 4798 | * @cur: the attribute pointer |
| 4799 | * |
| 4800 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4801 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4802 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4803 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4804 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4805 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4806 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4807 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4808 | xmlGenericError(xmlGenericErrorContext, |
| 4809 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4810 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4811 | return; |
| 4812 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4813 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4814 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 4815 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4816 | xmlBufferWriteChar(buf, ":"); |
| 4817 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4818 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4819 | value = xmlNodeListGetString(doc, cur->children, 0); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4820 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4821 | xmlBufferWriteChar(buf, "="); |
| 4822 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4823 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 4824 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4825 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4826 | } |
| 4827 | } |
| 4828 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4829 | /** |
| 4830 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4831 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4832 | * @doc: the document |
| 4833 | * @cur: the first attribute pointer |
| 4834 | * |
| 4835 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4836 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4837 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4838 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4839 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4840 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4841 | xmlGenericError(xmlGenericErrorContext, |
| 4842 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4843 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4844 | return; |
| 4845 | } |
| 4846 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4847 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4848 | cur = cur->next; |
| 4849 | } |
| 4850 | } |
| 4851 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4852 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4853 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4854 | /** |
| 4855 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4856 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4857 | * @doc: the document |
| 4858 | * @cur: the first node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4859 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4860 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4861 | * |
| 4862 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 4863 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4864 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4865 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4866 | int format) { |
| 4867 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4868 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4869 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4870 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4871 | xmlGenericError(xmlGenericErrorContext, |
| 4872 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4873 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4874 | return; |
| 4875 | } |
| 4876 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4877 | if ((format) && (xmlIndentTreeOutput) && |
| 4878 | (cur->type == XML_ELEMENT_NODE)) |
| 4879 | for (i = 0;i < level;i++) |
| 4880 | xmlBufferWriteChar(buf, " "); |
| 4881 | xmlNodeDump(buf, doc, cur, level, format); |
| 4882 | if (format) { |
| 4883 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4884 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4885 | cur = cur->next; |
| 4886 | } |
| 4887 | } |
| 4888 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4889 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4890 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4891 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4892 | * @doc: the document |
| 4893 | * @cur: the current node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4894 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4895 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4896 | * |
| 4897 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4898 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4899 | void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4900 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4901 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4902 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4903 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4904 | |
| 4905 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4906 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4907 | xmlGenericError(xmlGenericErrorContext, |
| 4908 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4909 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4910 | return; |
| 4911 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4912 | if (cur->type == XML_XINCLUDE_START) |
| 4913 | return; |
| 4914 | if (cur->type == XML_XINCLUDE_END) |
| 4915 | return; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4916 | if (cur->type == XML_DTD_NODE) { |
| 4917 | xmlDtdDump(buf, (xmlDtdPtr) cur); |
| 4918 | return; |
| 4919 | } |
| 4920 | if (cur->type == XML_ELEMENT_DECL) { |
| 4921 | xmlDumpElementDecl(buf, (xmlElementPtr) cur); |
| 4922 | return; |
| 4923 | } |
| 4924 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4925 | xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur); |
| 4926 | return; |
| 4927 | } |
| 4928 | if (cur->type == XML_ENTITY_DECL) { |
| 4929 | xmlDumpEntityDecl(buf, (xmlEntityPtr) cur); |
| 4930 | return; |
| 4931 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4932 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4933 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4934 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4935 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4936 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4937 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4938 | #else |
| 4939 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4940 | xmlBufferContent(cur->content)); |
| 4941 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4942 | if (buffer != NULL) { |
| 4943 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4944 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4945 | } |
| 4946 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4947 | return; |
| 4948 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4949 | if (cur->type == XML_PI_NODE) { |
| 4950 | if (cur->content != NULL) { |
| 4951 | xmlBufferWriteChar(buf, "<?"); |
| 4952 | xmlBufferWriteCHAR(buf, cur->name); |
| 4953 | if (cur->content != NULL) { |
| 4954 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4955 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4956 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4957 | #else |
| 4958 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4959 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4960 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4961 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4962 | } else { |
| 4963 | xmlBufferWriteChar(buf, "<?"); |
| 4964 | xmlBufferWriteCHAR(buf, cur->name); |
| 4965 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4966 | } |
| 4967 | return; |
| 4968 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4969 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4970 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4971 | xmlBufferWriteChar(buf, "<!--"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4972 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4973 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4974 | #else |
| 4975 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4976 | #endif |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4977 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4978 | } |
| 4979 | return; |
| 4980 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4981 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4982 | xmlBufferWriteChar(buf, "&"); |
| 4983 | xmlBufferWriteCHAR(buf, cur->name); |
| 4984 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4985 | return; |
| 4986 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4987 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 4988 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 4989 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4990 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4991 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4992 | #else |
| 4993 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4994 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4995 | xmlBufferWriteChar(buf, "]]>"); |
| 4996 | return; |
| 4997 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4998 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4999 | if (format == 1) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5000 | tmp = cur->children; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5001 | while (tmp != NULL) { |
| 5002 | if ((tmp->type == XML_TEXT_NODE) || |
| 5003 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5004 | format = 0; |
| 5005 | break; |
| 5006 | } |
| 5007 | tmp = tmp->next; |
| 5008 | } |
| 5009 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5010 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5011 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5012 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5013 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5016 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5017 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5018 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5019 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5020 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5021 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5022 | if ((cur->content == NULL) && (cur->children == NULL) && |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 5023 | (!xmlSaveNoEmptyTags)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5024 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5025 | return; |
| 5026 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5027 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5028 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5029 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5030 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5031 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5032 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5033 | #else |
| 5034 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5035 | xmlBufferContent(cur->content)); |
| 5036 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5037 | if (buffer != NULL) { |
| 5038 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 5039 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5040 | } |
| 5041 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5042 | if (cur->children != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5043 | if (format) xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5044 | xmlNodeListDump(buf, doc, cur->children, |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 5045 | (level >= 0?level+1:-1), format); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5046 | if ((xmlIndentTreeOutput) && (format)) |
| 5047 | for (i = 0;i < level;i++) |
| 5048 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5049 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5050 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5051 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5052 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5053 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5056 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5057 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5060 | /** |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5061 | * xmlElemDump: |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 5062 | * @f: the FILE * for the output |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5063 | * @doc: the document |
| 5064 | * @cur: the current node |
| 5065 | * |
| 5066 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 5067 | */ |
| 5068 | void |
| 5069 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 5070 | xmlBufferPtr buf; |
| 5071 | |
| 5072 | if (cur == NULL) { |
| 5073 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5074 | xmlGenericError(xmlGenericErrorContext, |
| 5075 | "xmlElemDump : cur == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5076 | #endif |
| 5077 | return; |
| 5078 | } |
| 5079 | if (doc == NULL) { |
| 5080 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5081 | xmlGenericError(xmlGenericErrorContext, |
| 5082 | "xmlElemDump : doc == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5083 | #endif |
| 5084 | } |
| 5085 | buf = xmlBufferCreate(); |
| 5086 | if (buf == NULL) return; |
| 5087 | if ((doc != NULL) && |
| 5088 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5089 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5090 | htmlNodeDump(buf, doc, cur); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5091 | #else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5092 | xmlGenericError(xmlGenericErrorContext, |
| 5093 | "HTML support not compiled in\n"); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5094 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5095 | } else |
| 5096 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 5097 | xmlBufferDump(f, buf); |
| 5098 | xmlBufferFree(buf); |
| 5099 | } |
| 5100 | |
| 5101 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5102 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 5103 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5104 | * @cur: the document |
| 5105 | * |
| 5106 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5107 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5108 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5109 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 5110 | xmlBufferWriteChar(buf, "<?xml version="); |
| 5111 | if (cur->version != NULL) |
| 5112 | xmlBufferWriteQuotedString(buf, cur->version); |
| 5113 | else |
| 5114 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5115 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 5116 | xmlBufferWriteChar(buf, " encoding="); |
| 5117 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5118 | } |
| 5119 | switch (cur->standalone) { |
| 5120 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5121 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5122 | break; |
| 5123 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5124 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5125 | break; |
| 5126 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5127 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5128 | if (cur->children != NULL) { |
| 5129 | xmlNodePtr child = cur->children; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5130 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5131 | while (child != NULL) { |
Daniel Veillard | bf43275 | 2000-11-12 15:56:56 +0000 | [diff] [blame] | 5132 | xmlNodeDump(buf, cur, child, 0, 0); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5133 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5134 | child = child->next; |
| 5135 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5136 | } |
| 5137 | } |
| 5138 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5139 | /************************************************************************ |
| 5140 | * * |
| 5141 | * Dumping XML tree content to an I/O output buffer * |
| 5142 | * * |
| 5143 | ************************************************************************/ |
| 5144 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5145 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5146 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5147 | int level, int format, const char *encoding); |
| 5148 | static void |
| 5149 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5150 | int level, int format, const char *encoding); |
| 5151 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5152 | * xmlNsDumpOutput: |
| 5153 | * @buf: the XML buffer output |
| 5154 | * @cur: a namespace |
| 5155 | * |
| 5156 | * Dump a local Namespace definition. |
| 5157 | * Should be called in the context of attributes dumps. |
| 5158 | */ |
| 5159 | static void |
| 5160 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5161 | if (cur == NULL) { |
| 5162 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5163 | xmlGenericError(xmlGenericErrorContext, |
| 5164 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5165 | #endif |
| 5166 | return; |
| 5167 | } |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 5168 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5169 | /* Within the context of an element attributes */ |
| 5170 | if (cur->prefix != NULL) { |
| 5171 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 5172 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 5173 | } else |
| 5174 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 5175 | xmlOutputBufferWriteString(buf, "="); |
| 5176 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | /** |
| 5181 | * xmlNsListDumpOutput: |
| 5182 | * @buf: the XML buffer output |
| 5183 | * @cur: the first namespace |
| 5184 | * |
| 5185 | * Dump a list of local Namespace definitions. |
| 5186 | * Should be called in the context of attributes dumps. |
| 5187 | */ |
| 5188 | static void |
| 5189 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5190 | while (cur != NULL) { |
| 5191 | xmlNsDumpOutput(buf, cur); |
| 5192 | cur = cur->next; |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | /** |
| 5197 | * xmlDtdDumpOutput: |
| 5198 | * @buf: the XML buffer output |
| 5199 | * @doc: the document |
| 5200 | * @encoding: an optional encoding string |
| 5201 | * |
| 5202 | * Dump the XML document DTD, if any. |
| 5203 | */ |
| 5204 | static void |
| 5205 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 5206 | if (dtd == NULL) { |
| 5207 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5208 | xmlGenericError(xmlGenericErrorContext, |
| 5209 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5210 | #endif |
| 5211 | return; |
| 5212 | } |
| 5213 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 5214 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 5215 | if (dtd->ExternalID != NULL) { |
| 5216 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 5217 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 5218 | xmlOutputBufferWriteString(buf, " "); |
| 5219 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5220 | } else if (dtd->SystemID != NULL) { |
| 5221 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 5222 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5223 | } |
| 5224 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 5225 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 5226 | xmlOutputBufferWriteString(buf, ">"); |
| 5227 | return; |
| 5228 | } |
| 5229 | xmlOutputBufferWriteString(buf, " [\n"); |
| 5230 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5231 | xmlOutputBufferWriteString(buf, "]>"); |
| 5232 | } |
| 5233 | |
| 5234 | /** |
| 5235 | * xmlAttrDumpOutput: |
| 5236 | * @buf: the XML buffer output |
| 5237 | * @doc: the document |
| 5238 | * @cur: the attribute pointer |
| 5239 | * @encoding: an optional encoding string |
| 5240 | * |
| 5241 | * Dump an XML attribute |
| 5242 | */ |
| 5243 | static void |
| 5244 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
| 5245 | const char *encoding) { |
| 5246 | xmlChar *value; |
| 5247 | |
| 5248 | if (cur == NULL) { |
| 5249 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5250 | xmlGenericError(xmlGenericErrorContext, |
| 5251 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5252 | #endif |
| 5253 | return; |
| 5254 | } |
| 5255 | xmlOutputBufferWriteString(buf, " "); |
| 5256 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5257 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5258 | xmlOutputBufferWriteString(buf, ":"); |
| 5259 | } |
| 5260 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5261 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 5262 | if (value) { |
| 5263 | xmlOutputBufferWriteString(buf, "="); |
| 5264 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 5265 | xmlFree(value); |
| 5266 | } else { |
| 5267 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 5268 | } |
| 5269 | } |
| 5270 | |
| 5271 | /** |
| 5272 | * xmlAttrListDumpOutput: |
| 5273 | * @buf: the XML buffer output |
| 5274 | * @doc: the document |
| 5275 | * @cur: the first attribute pointer |
| 5276 | * @encoding: an optional encoding string |
| 5277 | * |
| 5278 | * Dump a list of XML attributes |
| 5279 | */ |
| 5280 | static void |
| 5281 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5282 | xmlAttrPtr cur, const char *encoding) { |
| 5283 | if (cur == NULL) { |
| 5284 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5285 | xmlGenericError(xmlGenericErrorContext, |
| 5286 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5287 | #endif |
| 5288 | return; |
| 5289 | } |
| 5290 | while (cur != NULL) { |
| 5291 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 5292 | cur = cur->next; |
| 5293 | } |
| 5294 | } |
| 5295 | |
| 5296 | |
| 5297 | |
| 5298 | /** |
| 5299 | * xmlNodeListDumpOutput: |
| 5300 | * @buf: the XML buffer output |
| 5301 | * @doc: the document |
| 5302 | * @cur: the first node |
| 5303 | * @level: the imbrication level for indenting |
| 5304 | * @format: is formatting allowed |
| 5305 | * @encoding: an optional encoding string |
| 5306 | * |
| 5307 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 5308 | */ |
| 5309 | static void |
| 5310 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5311 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 5312 | int i; |
| 5313 | |
| 5314 | if (cur == NULL) { |
| 5315 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5316 | xmlGenericError(xmlGenericErrorContext, |
| 5317 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5318 | #endif |
| 5319 | return; |
| 5320 | } |
| 5321 | while (cur != NULL) { |
| 5322 | if ((format) && (xmlIndentTreeOutput) && |
| 5323 | (cur->type == XML_ELEMENT_NODE)) |
| 5324 | for (i = 0;i < level;i++) |
| 5325 | xmlOutputBufferWriteString(buf, " "); |
| 5326 | xmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 5327 | if (format) { |
| 5328 | xmlOutputBufferWriteString(buf, "\n"); |
| 5329 | } |
| 5330 | cur = cur->next; |
| 5331 | } |
| 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlNodeDumpOutput: |
| 5336 | * @buf: the XML buffer output |
| 5337 | * @doc: the document |
| 5338 | * @cur: the current node |
| 5339 | * @level: the imbrication level for indenting |
| 5340 | * @format: is formatting allowed |
| 5341 | * @encoding: an optional encoding string |
| 5342 | * |
| 5343 | * Dump an XML node, recursive behaviour,children are printed too. |
| 5344 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5345 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5346 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5347 | int level, int format, const char *encoding) { |
| 5348 | int i; |
| 5349 | xmlNodePtr tmp; |
| 5350 | |
| 5351 | if (cur == NULL) { |
| 5352 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5353 | xmlGenericError(xmlGenericErrorContext, |
| 5354 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5355 | #endif |
| 5356 | return; |
| 5357 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 5358 | if (cur->type == XML_XINCLUDE_START) |
| 5359 | return; |
| 5360 | if (cur->type == XML_XINCLUDE_END) |
| 5361 | return; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5362 | if (cur->type == XML_DTD_NODE) { |
| 5363 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 5364 | return; |
| 5365 | } |
| 5366 | if (cur->type == XML_ELEMENT_DECL) { |
| 5367 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 5368 | return; |
| 5369 | } |
| 5370 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5371 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 5372 | return; |
| 5373 | } |
| 5374 | if (cur->type == XML_ENTITY_DECL) { |
| 5375 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 5376 | return; |
| 5377 | } |
| 5378 | if (cur->type == XML_TEXT_NODE) { |
| 5379 | if (cur->content != NULL) { |
| 5380 | xmlChar *buffer; |
| 5381 | |
| 5382 | #ifndef XML_USE_BUFFER_CONTENT |
| 5383 | if (encoding == NULL) |
| 5384 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5385 | else |
| 5386 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5387 | #else |
| 5388 | if (encoding == NULL) |
| 5389 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5390 | xmlBufferContent(cur->content)); |
| 5391 | else |
| 5392 | buffer = xmlEncodeSpecialChars(doc, |
| 5393 | xmlBufferContent(cur->content)); |
| 5394 | #endif |
| 5395 | if (buffer != NULL) { |
| 5396 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5397 | xmlFree(buffer); |
| 5398 | } |
| 5399 | } |
| 5400 | return; |
| 5401 | } |
| 5402 | if (cur->type == XML_PI_NODE) { |
| 5403 | if (cur->content != NULL) { |
| 5404 | xmlOutputBufferWriteString(buf, "<?"); |
| 5405 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5406 | if (cur->content != NULL) { |
| 5407 | xmlOutputBufferWriteString(buf, " "); |
| 5408 | #ifndef XML_USE_BUFFER_CONTENT |
| 5409 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5410 | #else |
| 5411 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5412 | #endif |
| 5413 | } |
| 5414 | xmlOutputBufferWriteString(buf, "?>"); |
| 5415 | } else { |
| 5416 | xmlOutputBufferWriteString(buf, "<?"); |
| 5417 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5418 | xmlOutputBufferWriteString(buf, "?>"); |
| 5419 | } |
| 5420 | return; |
| 5421 | } |
| 5422 | if (cur->type == XML_COMMENT_NODE) { |
| 5423 | if (cur->content != NULL) { |
| 5424 | xmlOutputBufferWriteString(buf, "<!--"); |
| 5425 | #ifndef XML_USE_BUFFER_CONTENT |
| 5426 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5427 | #else |
| 5428 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5429 | #endif |
| 5430 | xmlOutputBufferWriteString(buf, "-->"); |
| 5431 | } |
| 5432 | return; |
| 5433 | } |
| 5434 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 5435 | xmlOutputBufferWriteString(buf, "&"); |
| 5436 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5437 | xmlOutputBufferWriteString(buf, ";"); |
| 5438 | return; |
| 5439 | } |
| 5440 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5441 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 5442 | if (cur->content != NULL) |
| 5443 | #ifndef XML_USE_BUFFER_CONTENT |
| 5444 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5445 | #else |
| 5446 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5447 | #endif |
| 5448 | xmlOutputBufferWriteString(buf, "]]>"); |
| 5449 | return; |
| 5450 | } |
| 5451 | |
| 5452 | if (format == 1) { |
| 5453 | tmp = cur->children; |
| 5454 | while (tmp != NULL) { |
| 5455 | if ((tmp->type == XML_TEXT_NODE) || |
| 5456 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5457 | format = 0; |
| 5458 | break; |
| 5459 | } |
| 5460 | tmp = tmp->next; |
| 5461 | } |
| 5462 | } |
| 5463 | xmlOutputBufferWriteString(buf, "<"); |
| 5464 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5465 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5466 | xmlOutputBufferWriteString(buf, ":"); |
| 5467 | } |
| 5468 | |
| 5469 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5470 | if (cur->nsDef) |
| 5471 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 5472 | if (cur->properties != NULL) |
| 5473 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 5474 | |
| 5475 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5476 | (!xmlSaveNoEmptyTags)) { |
| 5477 | xmlOutputBufferWriteString(buf, "/>"); |
| 5478 | return; |
| 5479 | } |
| 5480 | xmlOutputBufferWriteString(buf, ">"); |
| 5481 | if (cur->content != NULL) { |
| 5482 | xmlChar *buffer; |
| 5483 | |
| 5484 | #ifndef XML_USE_BUFFER_CONTENT |
| 5485 | if (encoding == NULL) |
| 5486 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5487 | else |
| 5488 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5489 | #else |
| 5490 | if (encoding == NULL) |
| 5491 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5492 | xmlBufferContent(cur->content)); |
| 5493 | else |
| 5494 | buffer = xmlEncodeSpecialChars(doc, |
| 5495 | xmlBufferContent(cur->content)); |
| 5496 | #endif |
| 5497 | if (buffer != NULL) { |
| 5498 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5499 | xmlFree(buffer); |
| 5500 | } |
| 5501 | } |
| 5502 | if (cur->children != NULL) { |
| 5503 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 5504 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 5505 | (level >= 0?level+1:-1), format, encoding); |
| 5506 | if ((xmlIndentTreeOutput) && (format)) |
| 5507 | for (i = 0;i < level;i++) |
| 5508 | xmlOutputBufferWriteString(buf, " "); |
| 5509 | } |
| 5510 | xmlOutputBufferWriteString(buf, "</"); |
| 5511 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5512 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5513 | xmlOutputBufferWriteString(buf, ":"); |
| 5514 | } |
| 5515 | |
| 5516 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5517 | xmlOutputBufferWriteString(buf, ">"); |
| 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * xmlDocContentDumpOutput: |
| 5522 | * @buf: the XML buffer output |
| 5523 | * @cur: the document |
| 5524 | * @encoding: an optional encoding string |
| 5525 | * |
| 5526 | * Dump an XML document. |
| 5527 | */ |
| 5528 | static void |
| 5529 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 5530 | const char *encoding) { |
| 5531 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 5532 | if (cur->version != NULL) |
| 5533 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 5534 | else |
| 5535 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 5536 | if (encoding == NULL) { |
| 5537 | if (cur->encoding != NULL) |
| 5538 | encoding = (const char *) cur->encoding; |
| 5539 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5540 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5541 | } |
| 5542 | if (encoding != NULL) { |
| 5543 | xmlOutputBufferWriteString(buf, " encoding="); |
| 5544 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 5545 | } |
| 5546 | switch (cur->standalone) { |
| 5547 | case 0: |
| 5548 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 5549 | break; |
| 5550 | case 1: |
| 5551 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 5552 | break; |
| 5553 | } |
| 5554 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 5555 | if (cur->children != NULL) { |
| 5556 | xmlNodePtr child = cur->children; |
| 5557 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5558 | while (child != NULL) { |
| 5559 | xmlNodeDumpOutput(buf, cur, child, 0, 1, encoding); |
| 5560 | xmlOutputBufferWriteString(buf, "\n"); |
| 5561 | child = child->next; |
| 5562 | } |
| 5563 | } |
| 5564 | } |
| 5565 | |
| 5566 | /************************************************************************ |
| 5567 | * * |
| 5568 | * Saving functions front-ends * |
| 5569 | * * |
| 5570 | ************************************************************************/ |
| 5571 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5572 | /** |
| 5573 | * xmlDocDumpMemory: |
| 5574 | * @cur: the document |
| 5575 | * @mem: OUT: the memory pointer |
| 5576 | * @size: OUT: the memory lenght |
| 5577 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5578 | * 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] | 5579 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5580 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5581 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5582 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5583 | xmlBufferPtr buf; |
| 5584 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5585 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5586 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5587 | xmlGenericError(xmlGenericErrorContext, |
| 5588 | "xmlDocDumpMemory : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5589 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5590 | *mem = NULL; |
| 5591 | *size = 0; |
| 5592 | return; |
| 5593 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5594 | buf = xmlBufferCreate(); |
| 5595 | if (buf == NULL) { |
| 5596 | *mem = NULL; |
| 5597 | *size = 0; |
| 5598 | return; |
| 5599 | } |
| 5600 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5601 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5602 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5603 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5606 | /** |
Daniel Veillard | 58770e7 | 2000-11-25 00:48:47 +0000 | [diff] [blame] | 5607 | * xmlDocDumpMemoryEnc: |
| 5608 | * @out_doc: Document to generate XML text from |
| 5609 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 5610 | * @doc_txt_len: Length of the generated XML text |
| 5611 | * @txt_encoding: Character encoding to use when generating XML text |
| 5612 | * |
| 5613 | * Dump the current DOM tree into memory using the character encoding specified |
| 5614 | * by the caller. Note it is up to the caller of this function to free the |
| 5615 | * allocated memory. |
| 5616 | */ |
| 5617 | |
| 5618 | void |
| 5619 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 5620 | int * doc_txt_len, const char * txt_encoding) { |
| 5621 | int dummy = 0; |
| 5622 | |
| 5623 | xmlCharEncoding doc_charset; |
| 5624 | xmlOutputBufferPtr out_buff = NULL; |
| 5625 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 5626 | |
| 5627 | if (doc_txt_len == NULL) { |
| 5628 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 5629 | } |
| 5630 | |
| 5631 | if (doc_txt_ptr == NULL) { |
| 5632 | *doc_txt_len = 0; |
| 5633 | xmlGenericError(xmlGenericErrorContext, |
| 5634 | "xmlDocDumpMemoryEnc: Null return buffer pointer."); |
| 5635 | return; |
| 5636 | } |
| 5637 | |
| 5638 | *doc_txt_ptr = NULL; |
| 5639 | *doc_txt_len = 0; |
| 5640 | |
| 5641 | if (out_doc == NULL) { |
| 5642 | /* No document, no output */ |
| 5643 | xmlGenericError(xmlGenericErrorContext, |
| 5644 | "xmlDocDumpMemoryEnc: Null DOM tree document pointer.\n"); |
| 5645 | return; |
| 5646 | } |
| 5647 | |
| 5648 | /* |
| 5649 | * Validate the encoding value, if provided. |
| 5650 | * This logic is copied from xmlSaveFileEnc. |
| 5651 | */ |
| 5652 | |
| 5653 | if (txt_encoding != NULL) { |
| 5654 | doc_charset = xmlParseCharEncoding(txt_encoding); |
| 5655 | |
| 5656 | if (out_doc->charset != XML_CHAR_ENCODING_UTF8) { |
| 5657 | xmlGenericError(xmlGenericErrorContext, |
| 5658 | "xmlDocDumpMemoryEnc: Source document not in UTF8\n"); |
| 5659 | return; |
| 5660 | |
| 5661 | } else if (doc_charset != XML_CHAR_ENCODING_UTF8) { |
| 5662 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 5663 | if ( conv_hdlr == NULL ) { |
| 5664 | xmlGenericError(xmlGenericErrorContext, |
| 5665 | "%s: %s %s '%s'\n", |
| 5666 | "xmlDocDumpMemoryEnc", |
| 5667 | "Failed to identify encoding handler for", |
| 5668 | "character set", |
| 5669 | txt_encoding); |
| 5670 | return; |
| 5671 | } |
| 5672 | } |
| 5673 | } |
| 5674 | |
| 5675 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 5676 | xmlGenericError(xmlGenericErrorContext, |
| 5677 | "xmlDocDumpMemoryEnc: Failed to allocate output buffer.\n"); |
| 5678 | return; |
| 5679 | } |
| 5680 | |
| 5681 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding); |
| 5682 | *doc_txt_len = out_buff->buffer->use; |
| 5683 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 5684 | (void)xmlOutputBufferClose(out_buff); |
| 5685 | |
| 5686 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 5687 | *doc_txt_len = 0; |
| 5688 | xmlGenericError(xmlGenericErrorContext, |
| 5689 | "xmlDocDumpMemoryEnc: %s\n", |
| 5690 | "Failed to allocate memory for document text representation."); |
| 5691 | } |
| 5692 | |
| 5693 | return; |
| 5694 | } |
| 5695 | |
| 5696 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5697 | * xmlGetDocCompressMode: |
| 5698 | * @doc: the document |
| 5699 | * |
| 5700 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5701 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5702 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5703 | int |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5704 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5705 | if (doc == NULL) return(-1); |
| 5706 | return(doc->compression); |
| 5707 | } |
| 5708 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5709 | /** |
| 5710 | * xmlSetDocCompressMode: |
| 5711 | * @doc: the document |
| 5712 | * @mode: the compression ratio |
| 5713 | * |
| 5714 | * set the compression ratio for a document, ZLIB based |
| 5715 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5716 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5717 | void |
| 5718 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5719 | if (doc == NULL) return; |
| 5720 | if (mode < 0) doc->compression = 0; |
| 5721 | else if (mode > 9) doc->compression = 9; |
| 5722 | else doc->compression = mode; |
| 5723 | } |
| 5724 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5725 | /** |
| 5726 | * xmlGetCompressMode: |
| 5727 | * |
| 5728 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5729 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5730 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5731 | int |
| 5732 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5733 | return(xmlCompressMode); |
| 5734 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5735 | |
| 5736 | /** |
| 5737 | * xmlSetCompressMode: |
| 5738 | * @mode: the compression ratio |
| 5739 | * |
| 5740 | * set the default compression mode used, ZLIB based |
| 5741 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5742 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5743 | void |
| 5744 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5745 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5746 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5747 | else xmlCompressMode = mode; |
| 5748 | } |
| 5749 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5750 | /** |
| 5751 | * xmlDocDump: |
| 5752 | * @f: the FILE* |
| 5753 | * @cur: the document |
| 5754 | * |
| 5755 | * Dump an XML document to an open FILE. |
| 5756 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5757 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5758 | */ |
| 5759 | int |
| 5760 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 5761 | xmlOutputBufferPtr buf; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5762 | const char * encoding; |
| 5763 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5764 | int ret; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5765 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5766 | if (cur == NULL) { |
| 5767 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5768 | xmlGenericError(xmlGenericErrorContext, |
| 5769 | "xmlDocDump : document == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5770 | #endif |
| 5771 | return(-1); |
| 5772 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5773 | encoding = (const char *) cur->encoding; |
| 5774 | |
| 5775 | if (encoding != NULL) { |
| 5776 | xmlCharEncoding enc; |
| 5777 | |
| 5778 | enc = xmlParseCharEncoding(encoding); |
| 5779 | |
| 5780 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5781 | xmlGenericError(xmlGenericErrorContext, |
| 5782 | "xmlDocDump: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5783 | return(-1); |
| 5784 | } |
| 5785 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5786 | handler = xmlFindCharEncodingHandler(encoding); |
| 5787 | if (handler == NULL) { |
| 5788 | xmlFree((char *) cur->encoding); |
| 5789 | cur->encoding = NULL; |
| 5790 | } |
| 5791 | } |
| 5792 | } |
| 5793 | buf = xmlOutputBufferCreateFile(f, handler); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5794 | if (buf == NULL) return(-1); |
| 5795 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5796 | |
| 5797 | ret = xmlOutputBufferClose(buf); |
| 5798 | return(ret); |
| 5799 | } |
| 5800 | |
| 5801 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5802 | * xmlSaveFileTo: |
| 5803 | * @buf: an output I/O buffer |
| 5804 | * @cur: the document |
| 5805 | * @encoding: the encoding if any assuming the i/O layer handles the trancoding |
| 5806 | * |
| 5807 | * Dump an XML document to an I/O buffer. |
| 5808 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5809 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5810 | */ |
| 5811 | int |
| 5812 | xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) { |
| 5813 | int ret; |
| 5814 | |
| 5815 | if (buf == NULL) return(0); |
| 5816 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5817 | ret = xmlOutputBufferClose(buf); |
| 5818 | return(ret); |
| 5819 | } |
| 5820 | |
| 5821 | /** |
| 5822 | * xmlSaveFileEnc: |
| 5823 | * @filename: the filename (or URL) |
| 5824 | * @cur: the document |
| 5825 | * @encoding: the name of an encoding (or NULL) |
| 5826 | * |
| 5827 | * Dump an XML document, converting it to the given encoding |
| 5828 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5829 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5830 | */ |
| 5831 | int |
| 5832 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 5833 | xmlOutputBufferPtr buf; |
| 5834 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5835 | int ret; |
| 5836 | |
| 5837 | if (encoding != NULL) { |
| 5838 | xmlCharEncoding enc; |
| 5839 | |
| 5840 | enc = xmlParseCharEncoding(encoding); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5841 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5842 | xmlGenericError(xmlGenericErrorContext, |
| 5843 | "xmlSaveFileEnc: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5844 | return(-1); |
| 5845 | } |
| 5846 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5847 | handler = xmlFindCharEncodingHandler(encoding); |
| 5848 | if (handler == NULL) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5849 | return(-1); |
| 5850 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | /* |
| 5855 | * save the content to a temp buffer. |
| 5856 | */ |
| 5857 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
| 5858 | if (buf == NULL) return(0); |
| 5859 | |
| 5860 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5861 | |
| 5862 | ret = xmlOutputBufferClose(buf); |
| 5863 | return(ret); |
| 5864 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5865 | |
| 5866 | /** |
| 5867 | * xmlSaveFile: |
| 5868 | * @filename: the filename (or URL) |
| 5869 | * @cur: the document |
| 5870 | * |
| 5871 | * Dump an XML document to a file. Will use compression if |
| 5872 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 5873 | * used. |
| 5874 | * returns: the number of byte written or -1 in case of failure. |
| 5875 | */ |
| 5876 | int |
| 5877 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 5878 | xmlOutputBufferPtr buf; |
| 5879 | const char *encoding; |
| 5880 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5881 | int ret; |
| 5882 | |
| 5883 | if (cur == NULL) |
| 5884 | return(-1); |
| 5885 | encoding = (const char *) cur->encoding; |
| 5886 | |
| 5887 | /* |
| 5888 | * save the content to a temp buffer. |
| 5889 | */ |
| 5890 | #ifdef HAVE_ZLIB_H |
| 5891 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5892 | #endif |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5893 | if (encoding != NULL) { |
| 5894 | xmlCharEncoding enc; |
| 5895 | |
| 5896 | enc = xmlParseCharEncoding(encoding); |
| 5897 | |
| 5898 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5899 | xmlGenericError(xmlGenericErrorContext, |
| 5900 | "xmlSaveFile: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5901 | return(-1); |
| 5902 | } |
| 5903 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5904 | handler = xmlFindCharEncodingHandler(encoding); |
| 5905 | if (handler == NULL) { |
| 5906 | xmlFree((char *) cur->encoding); |
| 5907 | cur->encoding = NULL; |
| 5908 | } |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 5913 | if (buf == NULL) return(0); |
| 5914 | |
| 5915 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5916 | |
| 5917 | ret = xmlOutputBufferClose(buf); |
| 5918 | return(ret); |
| 5919 | } |
| 5920 | |