Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tree.c : implemetation of access function for an XML tree. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 16 | #include <string.h> /* for memset() only ! */ |
| 17 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | #ifdef HAVE_CTYPE_H |
| 19 | #include <ctype.h> |
| 20 | #endif |
| 21 | #ifdef HAVE_STDLIB_H |
| 22 | #include <stdlib.h> |
| 23 | #endif |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 24 | #ifdef HAVE_ZLIB_H |
| 25 | #include <zlib.h> |
| 26 | #endif |
| 27 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 28 | #include <libxml/xmlmemory.h> |
| 29 | #include <libxml/tree.h> |
| 30 | #include <libxml/parser.h> |
| 31 | #include <libxml/entities.h> |
| 32 | #include <libxml/valid.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 33 | #include <libxml/xmlerror.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 35 | static xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 36 | static xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 37 | int oldXMLWDcompatibility = 0; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 38 | int xmlIndentTreeOutput = 0; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 39 | xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 40 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 41 | static int xmlCompressMode = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 42 | static int xmlCheckDTD = 1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 43 | int xmlSaveNoEmptyTags = 0; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 44 | |
| 45 | #define IS_BLANK(c) \ |
| 46 | (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' ')) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 47 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 48 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 49 | xmlNodePtr ulccur = (n)->children; \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 50 | if (ulccur == NULL) { \ |
| 51 | (n)->last = NULL; \ |
| 52 | } else { \ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 53 | while (ulccur->next != NULL) { \ |
| 54 | ulccur->parent = (n); \ |
| 55 | ulccur = ulccur->next; \ |
| 56 | } \ |
| 57 | ulccur->parent = (n); \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 58 | (n)->last = ulccur; \ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 59 | }} |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 60 | |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 61 | /* #define DEBUG_BUFFER */ |
| 62 | /* #define DEBUG_TREE */ |
| 63 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 64 | /************************************************************************ |
| 65 | * * |
| 66 | * Allocation and deallocation of basic structures * |
| 67 | * * |
| 68 | ************************************************************************/ |
| 69 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 70 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 71 | * xmlSetBufferAllocationScheme: |
| 72 | * @scheme: allocation method to use |
| 73 | * |
| 74 | * Set the buffer allocation method. Types are |
| 75 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 76 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 77 | * improves performance |
| 78 | */ |
| 79 | void |
| 80 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 81 | xmlBufferAllocScheme = scheme; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * xmlGetBufferAllocationScheme: |
| 86 | * |
| 87 | * Types are |
| 88 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 89 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 90 | * improves performance |
| 91 | * |
| 92 | * Returns the current allocation scheme |
| 93 | */ |
| 94 | xmlBufferAllocationScheme |
| 95 | xmlGetBufferAllocationScheme() { |
| 96 | return xmlBufferAllocScheme; |
| 97 | } |
| 98 | |
| 99 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 100 | * xmlNewNs: |
| 101 | * @node: the element carrying the namespace |
| 102 | * @href: the URI associated |
| 103 | * @prefix: the prefix for the namespace |
| 104 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 105 | * Creation of a new Namespace. This function will refuse to create |
| 106 | * a namespace with a similar prefix than an existing one present on this |
| 107 | * node. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 108 | * We use href==NULL in the case of an element creation where the namespace |
| 109 | * was not defined. |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 110 | * Returns returns a new namespace pointer or NULL |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 111 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 112 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 113 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 114 | xmlNsPtr cur; |
| 115 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 116 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 117 | * Allocate a new Namespace and fill the fields. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 118 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 119 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 120 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 121 | xmlGenericError(xmlGenericErrorContext, |
| 122 | "xmlNewNs : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 123 | return(NULL); |
| 124 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 125 | memset(cur, 0, sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 126 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 127 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 128 | if (href != NULL) |
| 129 | cur->href = xmlStrdup(href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 130 | if (prefix != NULL) |
| 131 | cur->prefix = xmlStrdup(prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Add it at the end to preserve parsing order ... |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 135 | * and checks for existing use of the prefix |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 136 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 137 | if (node != NULL) { |
| 138 | if (node->nsDef == NULL) { |
| 139 | node->nsDef = cur; |
| 140 | } else { |
| 141 | xmlNsPtr prev = node->nsDef; |
| 142 | |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 143 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 144 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 145 | xmlFreeNs(cur); |
| 146 | return(NULL); |
| 147 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 148 | while (prev->next != NULL) { |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 149 | prev = prev->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 150 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 151 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 152 | xmlFreeNs(cur); |
| 153 | return(NULL); |
| 154 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 155 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 156 | prev->next = cur; |
| 157 | } |
| 158 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 159 | return(cur); |
| 160 | } |
| 161 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 162 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 163 | * xmlSetNs: |
| 164 | * @node: a node in the document |
| 165 | * @ns: a namespace pointer |
| 166 | * |
| 167 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 168 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 169 | void |
| 170 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 171 | if (node == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 172 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 173 | xmlGenericError(xmlGenericErrorContext, |
| 174 | "xmlSetNs: node == NULL\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 175 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | node->ns = ns; |
| 179 | } |
| 180 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 181 | /** |
| 182 | * xmlFreeNs: |
| 183 | * @cur: the namespace pointer |
| 184 | * |
| 185 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 186 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 187 | void |
| 188 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 189 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 190 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 191 | xmlGenericError(xmlGenericErrorContext, |
| 192 | "xmlFreeNs : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 193 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 196 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 197 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 198 | memset(cur, -1, sizeof(xmlNs)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 199 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 202 | /** |
| 203 | * xmlFreeNsList: |
| 204 | * @cur: the first namespace pointer |
| 205 | * |
| 206 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 207 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 208 | void |
| 209 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 210 | xmlNsPtr next; |
| 211 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 212 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 213 | xmlGenericError(xmlGenericErrorContext, |
| 214 | "xmlFreeNsList : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 215 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | while (cur != NULL) { |
| 219 | next = cur->next; |
| 220 | xmlFreeNs(cur); |
| 221 | cur = next; |
| 222 | } |
| 223 | } |
| 224 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 225 | /** |
| 226 | * xmlNewDtd: |
| 227 | * @doc: the document pointer |
| 228 | * @name: the DTD name |
| 229 | * @ExternalID: the external ID |
| 230 | * @SystemID: the system ID |
| 231 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 232 | * Creation of a new DTD for the external subset. To create an |
| 233 | * internal subset, use xmlCreateIntSubset(). |
| 234 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 235 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 236 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 237 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 238 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 239 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 240 | xmlDtdPtr cur; |
| 241 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 242 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 243 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 244 | xmlGenericError(xmlGenericErrorContext, |
| 245 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 246 | /* !!! */ (char *) name, doc->name, |
| 247 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 248 | #endif |
| 249 | return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Allocate a new DTD and fill the fields. |
| 254 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 255 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 256 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 257 | xmlGenericError(xmlGenericErrorContext, |
| 258 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 259 | return(NULL); |
| 260 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 261 | memset(cur, 0 , sizeof(xmlDtd)); |
| 262 | cur->type = XML_DTD_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 263 | |
| 264 | if (name != NULL) |
| 265 | cur->name = xmlStrdup(name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 266 | if (ExternalID != NULL) |
| 267 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 268 | if (SystemID != NULL) |
| 269 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 270 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 271 | doc->extSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 272 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 273 | |
| 274 | return(cur); |
| 275 | } |
| 276 | |
| 277 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 278 | * xmlGetIntSubset: |
| 279 | * @doc: the document pointer |
| 280 | * |
| 281 | * Get the internal subset of a document |
| 282 | * Returns a pointer to the DTD structure or NULL if not found |
| 283 | */ |
| 284 | |
| 285 | xmlDtdPtr |
| 286 | xmlGetIntSubset(xmlDocPtr doc) { |
| 287 | xmlNodePtr cur; |
| 288 | |
| 289 | if (doc == NULL) |
| 290 | return(NULL); |
| 291 | cur = doc->children; |
| 292 | while (cur != NULL) { |
| 293 | if (cur->type == XML_DTD_NODE) |
| 294 | return((xmlDtdPtr) cur); |
| 295 | cur = cur->next; |
| 296 | } |
| 297 | return((xmlDtdPtr) doc->intSubset); |
| 298 | } |
| 299 | |
| 300 | /** |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 301 | * xmlCreateIntSubset: |
| 302 | * @doc: the document pointer |
| 303 | * @name: the DTD name |
| 304 | * @ExternalID: the external ID |
| 305 | * @SystemID: the system ID |
| 306 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 307 | * Create the internal subset of a document |
| 308 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 309 | */ |
| 310 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 311 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 312 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 313 | xmlDtdPtr cur; |
| 314 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 315 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 316 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 317 | xmlGenericError(xmlGenericErrorContext, |
| 318 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 319 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 320 | doc->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 321 | #endif |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 322 | return(NULL); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Allocate a new DTD and fill the fields. |
| 327 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 328 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 329 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 330 | xmlGenericError(xmlGenericErrorContext, |
| 331 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 332 | return(NULL); |
| 333 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 334 | memset(cur, 0, sizeof(xmlDtd)); |
| 335 | cur->type = XML_DTD_NODE; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 336 | |
| 337 | if (name != NULL) |
| 338 | cur->name = xmlStrdup(name); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 339 | if (ExternalID != NULL) |
| 340 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 341 | if (SystemID != NULL) |
| 342 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 343 | if (doc != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 344 | doc->intSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 345 | cur->parent = doc; |
| 346 | cur->doc = doc; |
| 347 | if (doc->children == NULL) { |
| 348 | doc->children = (xmlNodePtr) cur; |
| 349 | doc->last = (xmlNodePtr) cur; |
| 350 | } else { |
| 351 | xmlNodePtr prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 352 | |
Daniel Veillard | b8f25c9 | 2000-08-19 19:52:36 +0000 | [diff] [blame] | 353 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 354 | prev = doc->children; |
| 355 | prev->prev = (xmlNodePtr) cur; |
| 356 | cur->next = prev; |
| 357 | doc->children = (xmlNodePtr) cur; |
| 358 | } else { |
| 359 | prev = doc->last; |
| 360 | prev->next = (xmlNodePtr) cur; |
| 361 | cur->prev = prev; |
| 362 | doc->last = (xmlNodePtr) cur; |
| 363 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 366 | return(cur); |
| 367 | } |
| 368 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 369 | /** |
| 370 | * xmlFreeDtd: |
| 371 | * @cur: the DTD structure to free up |
| 372 | * |
| 373 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 374 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 375 | void |
| 376 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 377 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 378 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 379 | xmlGenericError(xmlGenericErrorContext, |
| 380 | "xmlFreeDtd : DTD == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 381 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 382 | return; |
| 383 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 384 | if (cur->children != NULL) { |
| 385 | xmlNodePtr next, c = cur->children; |
| 386 | |
| 387 | /* |
| 388 | * Cleanup all the DTD comments they are not in the Dtd |
| 389 | * indexes. |
| 390 | */ |
| 391 | while (c != NULL) { |
| 392 | next = c->next; |
| 393 | if (c->type == XML_COMMENT_NODE) { |
| 394 | xmlUnlinkNode(c); |
| 395 | xmlFreeNode(c); |
| 396 | } |
| 397 | c = next; |
| 398 | } |
| 399 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 400 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 401 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 402 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 403 | /* TODO !!! */ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 404 | if (cur->notations != NULL) |
| 405 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 406 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 407 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 408 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 409 | if (cur->attributes != NULL) |
| 410 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 411 | if (cur->entities != NULL) |
| 412 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 413 | if (cur->pentities != NULL) |
| 414 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 415 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 416 | memset(cur, -1, sizeof(xmlDtd)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 417 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 420 | /** |
| 421 | * xmlNewDoc: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 422 | * @version: xmlChar string giving the version of XML "1.0" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 423 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 424 | * Returns a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 425 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 426 | xmlDocPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 427 | xmlNewDoc(const xmlChar *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 428 | xmlDocPtr cur; |
| 429 | |
Daniel Veillard | 7eda845 | 2000-10-14 23:38:43 +0000 | [diff] [blame] | 430 | if (version == NULL) |
| 431 | version = (const xmlChar *) "1.0"; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Allocate a new document and fill the fields. |
| 435 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 436 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 437 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 438 | xmlGenericError(xmlGenericErrorContext, |
| 439 | "xmlNewDoc : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 440 | return(NULL); |
| 441 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 442 | memset(cur, 0, sizeof(xmlDoc)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 443 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 444 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 445 | cur->version = xmlStrdup(version); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 446 | cur->standalone = -1; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 447 | cur->compression = -1; /* not initialized */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 448 | cur->doc = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 449 | return(cur); |
| 450 | } |
| 451 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 452 | /** |
| 453 | * xmlFreeDoc: |
| 454 | * @cur: pointer to the document |
| 455 | * @: |
| 456 | * |
| 457 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 458 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 459 | void |
| 460 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 461 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 462 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 463 | xmlGenericError(xmlGenericErrorContext, |
| 464 | "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 465 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 468 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 469 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 470 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 471 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 472 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 473 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 474 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 475 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 476 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 477 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 478 | memset(cur, -1, sizeof(xmlDoc)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 479 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 482 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 483 | * xmlStringLenGetNodeList: |
| 484 | * @doc: the document |
| 485 | * @value: the value of the text |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 486 | * @len: the length of the string value |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 487 | * |
| 488 | * Parse the value string and build the node list associated. Should |
| 489 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 490 | * Returns a pointer to the first child |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 491 | */ |
| 492 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 493 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 494 | xmlNodePtr ret = NULL, last = NULL; |
| 495 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 496 | xmlChar *val; |
| 497 | const xmlChar *cur = value; |
| 498 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 499 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 500 | |
| 501 | if (value == NULL) return(NULL); |
| 502 | |
| 503 | q = cur; |
| 504 | while ((*cur != 0) && (cur - value < len)) { |
| 505 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 506 | /* |
| 507 | * Save the current text. |
| 508 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 509 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 510 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 511 | xmlNodeAddContentLen(last, q, cur - q); |
| 512 | } else { |
| 513 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 514 | if (node == NULL) return(ret); |
| 515 | if (last == NULL) |
| 516 | last = ret = node; |
| 517 | else { |
| 518 | last->next = node; |
| 519 | node->prev = last; |
| 520 | last = node; |
| 521 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 524 | /* |
| 525 | * Read the entity string |
| 526 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 527 | cur++; |
| 528 | q = cur; |
| 529 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 530 | if ((*cur == 0) || (cur - value >= len)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 531 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 532 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 533 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 534 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 535 | return(ret); |
| 536 | } |
| 537 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 538 | /* |
| 539 | * Predefined entities don't generate nodes |
| 540 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 541 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 542 | ent = xmlGetDocEntity(doc, val); |
| 543 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 544 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 545 | if (last == NULL) { |
| 546 | node = xmlNewDocText(doc, ent->content); |
| 547 | last = ret = node; |
| 548 | } else |
| 549 | xmlNodeAddContent(last, ent->content); |
| 550 | |
| 551 | } else { |
| 552 | /* |
| 553 | * Create a new REFERENCE_REF node |
| 554 | */ |
| 555 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 556 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 557 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 558 | return(ret); |
| 559 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 560 | if (last == NULL) |
| 561 | last = ret = node; |
| 562 | else { |
| 563 | last->next = node; |
| 564 | node->prev = last; |
| 565 | last = node; |
| 566 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 567 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 568 | xmlFree(val); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 569 | } |
| 570 | cur++; |
| 571 | q = cur; |
| 572 | } else |
| 573 | cur++; |
| 574 | } |
| 575 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 576 | /* |
| 577 | * Handle the last piece of text. |
| 578 | */ |
| 579 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 580 | xmlNodeAddContentLen(last, q, cur - q); |
| 581 | } else { |
| 582 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 583 | if (node == NULL) return(ret); |
| 584 | if (last == NULL) |
| 585 | last = ret = node; |
| 586 | else { |
| 587 | last->next = node; |
| 588 | node->prev = last; |
| 589 | last = node; |
| 590 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | return(ret); |
| 594 | } |
| 595 | |
| 596 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 597 | * xmlStringGetNodeList: |
| 598 | * @doc: the document |
| 599 | * @value: the value of the attribute |
| 600 | * |
| 601 | * Parse the value string and build the node list associated. Should |
| 602 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 603 | * Returns a pointer to the first child |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 604 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 605 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 606 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 607 | xmlNodePtr ret = NULL, last = NULL; |
| 608 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 609 | xmlChar *val; |
| 610 | const xmlChar *cur = value; |
| 611 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 612 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 613 | |
| 614 | if (value == NULL) return(NULL); |
| 615 | |
| 616 | q = cur; |
| 617 | while (*cur != 0) { |
| 618 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 619 | /* |
| 620 | * Save the current text. |
| 621 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 622 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 623 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 624 | xmlNodeAddContentLen(last, q, cur - q); |
| 625 | } else { |
| 626 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 627 | if (node == NULL) return(ret); |
| 628 | if (last == NULL) |
| 629 | last = ret = node; |
| 630 | else { |
| 631 | last->next = node; |
| 632 | node->prev = last; |
| 633 | last = node; |
| 634 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 637 | /* |
| 638 | * Read the entity string |
| 639 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 640 | cur++; |
| 641 | q = cur; |
| 642 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 643 | if (*cur == 0) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 644 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 645 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 646 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 647 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 648 | return(ret); |
| 649 | } |
| 650 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 651 | /* |
| 652 | * Predefined entities don't generate nodes |
| 653 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 654 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 655 | ent = xmlGetDocEntity(doc, val); |
| 656 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 657 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 658 | if (last == NULL) { |
| 659 | node = xmlNewDocText(doc, ent->content); |
| 660 | last = ret = node; |
| 661 | } else |
| 662 | xmlNodeAddContent(last, ent->content); |
| 663 | |
| 664 | } else { |
| 665 | /* |
| 666 | * Create a new REFERENCE_REF node |
| 667 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 668 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 669 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 670 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 671 | return(ret); |
| 672 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 673 | if (last == NULL) |
| 674 | last = ret = node; |
| 675 | else { |
| 676 | last->next = node; |
| 677 | node->prev = last; |
| 678 | last = node; |
| 679 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 680 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 681 | xmlFree(val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 682 | } |
| 683 | cur++; |
| 684 | q = cur; |
| 685 | } else |
| 686 | cur++; |
| 687 | } |
| 688 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 689 | /* |
| 690 | * Handle the last piece of text. |
| 691 | */ |
| 692 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 693 | xmlNodeAddContentLen(last, q, cur - q); |
| 694 | } else { |
| 695 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 696 | if (node == NULL) return(ret); |
| 697 | if (last == NULL) |
| 698 | last = ret = node; |
| 699 | else { |
| 700 | last->next = node; |
| 701 | node->prev = last; |
| 702 | last = node; |
| 703 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | return(ret); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * xmlNodeListGetString: |
| 711 | * @doc: the document |
| 712 | * @list: a Node list |
| 713 | * @inLine: should we replace entity contents or show their external form |
| 714 | * |
| 715 | * Returns the string equivalent to the text contained in the Node list |
| 716 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 717 | * Returns a pointer to the string copy, the calller must free it. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 718 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 719 | xmlChar * |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 720 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 721 | xmlNodePtr node = list; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 722 | xmlChar *ret = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 723 | xmlEntityPtr ent; |
| 724 | |
| 725 | if (list == NULL) return(NULL); |
| 726 | |
| 727 | while (node != NULL) { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 728 | if ((node->type == XML_TEXT_NODE) || |
| 729 | (node->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 730 | if (inLine) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 731 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 732 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 733 | #else |
| 734 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 735 | #endif |
| 736 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 737 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 738 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 739 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 740 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 741 | #else |
| 742 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 743 | xmlBufferContent(node->content)); |
| 744 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 745 | if (buffer != NULL) { |
| 746 | ret = xmlStrcat(ret, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 747 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 750 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 751 | if (inLine) { |
| 752 | ent = xmlGetDocEntity(doc, node->name); |
| 753 | if (ent != NULL) |
| 754 | ret = xmlStrcat(ret, ent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 755 | else { |
| 756 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 757 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 758 | #else |
| 759 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 760 | #endif |
| 761 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 762 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 763 | xmlChar buf[2]; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 764 | buf[0] = '&'; buf[1] = 0; |
| 765 | ret = xmlStrncat(ret, buf, 1); |
| 766 | ret = xmlStrcat(ret, node->name); |
| 767 | buf[0] = ';'; buf[1] = 0; |
| 768 | ret = xmlStrncat(ret, buf, 1); |
| 769 | } |
| 770 | } |
| 771 | #if 0 |
| 772 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 773 | xmlGenericError(xmlGenericErrorContext, |
| 774 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 775 | node->type); |
| 776 | } |
| 777 | #endif |
| 778 | node = node->next; |
| 779 | } |
| 780 | return(ret); |
| 781 | } |
| 782 | |
| 783 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 784 | * xmlNodeListGetRawString: |
| 785 | * @doc: the document |
| 786 | * @list: a Node list |
| 787 | * @inLine: should we replace entity contents or show their external form |
| 788 | * |
| 789 | * Returns the string equivalent to the text contained in the Node list |
| 790 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 791 | * this function doesn't do any character encoding handling. |
| 792 | * |
| 793 | * Returns a pointer to the string copy, the calller must free it. |
| 794 | */ |
| 795 | xmlChar * |
| 796 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 797 | xmlNodePtr node = list; |
| 798 | xmlChar *ret = NULL; |
| 799 | xmlEntityPtr ent; |
| 800 | |
| 801 | if (list == NULL) return(NULL); |
| 802 | |
| 803 | while (node != NULL) { |
| 804 | if (node->type == XML_TEXT_NODE) { |
| 805 | if (inLine) { |
| 806 | #ifndef XML_USE_BUFFER_CONTENT |
| 807 | ret = xmlStrcat(ret, node->content); |
| 808 | #else |
| 809 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 810 | #endif |
| 811 | } else { |
| 812 | xmlChar *buffer; |
| 813 | |
| 814 | #ifndef XML_USE_BUFFER_CONTENT |
| 815 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 816 | #else |
| 817 | buffer = xmlEncodeSpecialChars(doc, |
| 818 | xmlBufferContent(node->content)); |
| 819 | #endif |
| 820 | if (buffer != NULL) { |
| 821 | ret = xmlStrcat(ret, buffer); |
| 822 | xmlFree(buffer); |
| 823 | } |
| 824 | } |
| 825 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 826 | if (inLine) { |
| 827 | ent = xmlGetDocEntity(doc, node->name); |
| 828 | if (ent != NULL) |
| 829 | ret = xmlStrcat(ret, ent->content); |
| 830 | else { |
| 831 | #ifndef XML_USE_BUFFER_CONTENT |
| 832 | ret = xmlStrcat(ret, node->content); |
| 833 | #else |
| 834 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 835 | #endif |
| 836 | } |
| 837 | } else { |
| 838 | xmlChar buf[2]; |
| 839 | buf[0] = '&'; buf[1] = 0; |
| 840 | ret = xmlStrncat(ret, buf, 1); |
| 841 | ret = xmlStrcat(ret, node->name); |
| 842 | buf[0] = ';'; buf[1] = 0; |
| 843 | ret = xmlStrncat(ret, buf, 1); |
| 844 | } |
| 845 | } |
| 846 | #if 0 |
| 847 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 848 | xmlGenericError(xmlGenericErrorContext, |
| 849 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 850 | node->type); |
| 851 | } |
| 852 | #endif |
| 853 | node = node->next; |
| 854 | } |
| 855 | return(ret); |
| 856 | } |
| 857 | |
| 858 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 859 | * xmlNewProp: |
| 860 | * @node: the holding node |
| 861 | * @name: the name of the attribute |
| 862 | * @value: the value of the attribute |
| 863 | * |
| 864 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 865 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 866 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 867 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 868 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 869 | xmlAttrPtr cur; |
| 870 | |
| 871 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 872 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 873 | xmlGenericError(xmlGenericErrorContext, |
| 874 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 875 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 876 | return(NULL); |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Allocate a new property and fill the fields. |
| 881 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 882 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 883 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 884 | xmlGenericError(xmlGenericErrorContext, |
| 885 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 886 | return(NULL); |
| 887 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 888 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 889 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 890 | |
| 891 | cur->parent = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 892 | cur->name = xmlStrdup(name); |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 893 | if (value != NULL) { |
| 894 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 895 | xmlNodePtr tmp; |
| 896 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 897 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 898 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 899 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 900 | tmp = cur->children; |
| 901 | while (tmp != NULL) { |
| 902 | tmp->parent = (xmlNodePtr) cur; |
| 903 | if (tmp->next == NULL) |
| 904 | cur->last = tmp; |
| 905 | tmp = tmp->next; |
| 906 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 907 | xmlFree(buffer); |
| 908 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * Add it at the end to preserve parsing order ... |
| 912 | */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 913 | if (node != NULL) { |
| 914 | if (node->properties == NULL) { |
| 915 | node->properties = cur; |
| 916 | } else { |
| 917 | xmlAttrPtr prev = node->properties; |
| 918 | |
| 919 | while (prev->next != NULL) prev = prev->next; |
| 920 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 921 | cur->prev = prev; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | return(cur); |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * xmlNewNsProp: |
| 929 | * @node: the holding node |
| 930 | * @ns: the namespace |
| 931 | * @name: the name of the attribute |
| 932 | * @value: the value of the attribute |
| 933 | * |
| 934 | * Create a new property tagged with a namespace and carried by a node. |
| 935 | * Returns a pointer to the attribute |
| 936 | */ |
| 937 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 938 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 939 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 940 | xmlAttrPtr cur; |
| 941 | |
| 942 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 943 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 944 | xmlGenericError(xmlGenericErrorContext, |
| 945 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 946 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 947 | return(NULL); |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Allocate a new property and fill the fields. |
| 952 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 953 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 954 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 955 | xmlGenericError(xmlGenericErrorContext, |
| 956 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 957 | return(NULL); |
| 958 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 959 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 960 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 961 | |
| 962 | cur->parent = node; |
| 963 | if (node != NULL) |
| 964 | cur->doc = node->doc; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 965 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 966 | cur->name = xmlStrdup(name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 967 | if (value != NULL) { |
| 968 | xmlChar *buffer; |
| 969 | xmlNodePtr tmp; |
| 970 | |
| 971 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 972 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 973 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 974 | tmp = cur->children; |
| 975 | while (tmp != NULL) { |
| 976 | tmp->parent = (xmlNodePtr) cur; |
| 977 | if (tmp->next == NULL) |
| 978 | cur->last = tmp; |
| 979 | tmp = tmp->next; |
| 980 | } |
| 981 | xmlFree(buffer); |
| 982 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 983 | |
| 984 | /* |
| 985 | * Add it at the end to preserve parsing order ... |
| 986 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 987 | if (node != NULL) { |
| 988 | if (node->properties == NULL) { |
| 989 | node->properties = cur; |
| 990 | } else { |
| 991 | xmlAttrPtr prev = node->properties; |
| 992 | |
| 993 | while (prev->next != NULL) prev = prev->next; |
| 994 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 995 | cur->prev = prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | return(cur); |
| 999 | } |
| 1000 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1001 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1002 | * xmlNewDocProp: |
| 1003 | * @doc: the document |
| 1004 | * @name: the name of the attribute |
| 1005 | * @value: the value of the attribute |
| 1006 | * |
| 1007 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1008 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1009 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1010 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1011 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1012 | xmlAttrPtr cur; |
| 1013 | |
| 1014 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1015 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1016 | xmlGenericError(xmlGenericErrorContext, |
| 1017 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1018 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1019 | return(NULL); |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Allocate a new property and fill the fields. |
| 1024 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1025 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1026 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1027 | xmlGenericError(xmlGenericErrorContext, |
| 1028 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1029 | return(NULL); |
| 1030 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1031 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1032 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1033 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1034 | cur->name = xmlStrdup(name); |
| 1035 | cur->doc = doc; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1036 | if (value != NULL) { |
| 1037 | xmlNodePtr tmp; |
| 1038 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1039 | cur->children = xmlStringGetNodeList(doc, value); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1040 | cur->last = NULL; |
| 1041 | |
| 1042 | tmp = cur->children; |
| 1043 | while (tmp != NULL) { |
| 1044 | tmp->parent = (xmlNodePtr) cur; |
| 1045 | if (tmp->next == NULL) |
| 1046 | cur->last = tmp; |
| 1047 | tmp = tmp->next; |
| 1048 | } |
| 1049 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1050 | return(cur); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1054 | * xmlFreePropList: |
| 1055 | * @cur: the first property in the list |
| 1056 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1057 | * 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] | 1058 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1059 | void |
| 1060 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1061 | xmlAttrPtr next; |
| 1062 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1063 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1064 | xmlGenericError(xmlGenericErrorContext, |
| 1065 | "xmlFreePropList : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1066 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1067 | return; |
| 1068 | } |
| 1069 | while (cur != NULL) { |
| 1070 | next = cur->next; |
| 1071 | xmlFreeProp(cur); |
| 1072 | cur = next; |
| 1073 | } |
| 1074 | } |
| 1075 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1076 | /** |
| 1077 | * xmlFreeProp: |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1078 | * @cur: an attribute |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1079 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1080 | * Free one attribute, all the content is freed too |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1081 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1082 | void |
| 1083 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1084 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1085 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1086 | xmlGenericError(xmlGenericErrorContext, |
| 1087 | "xmlFreeProp : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1088 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1089 | return; |
| 1090 | } |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 1091 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1092 | if ((cur->parent != NULL) && |
| 1093 | (xmlIsID(cur->parent->doc, cur->parent, cur))) |
| 1094 | xmlRemoveID(cur->parent->doc, cur); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1095 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1096 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1097 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1098 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1101 | /** |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1102 | * xmlRemoveProp: |
| 1103 | * @cur: an attribute |
| 1104 | * |
| 1105 | * Unlink and free one attribute, all the content is freed too |
| 1106 | * Note this doesn't work for namespace definition attributes |
| 1107 | * |
| 1108 | * Returns 0 if success and -1 in case of error. |
| 1109 | */ |
| 1110 | int |
| 1111 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1112 | xmlAttrPtr tmp; |
| 1113 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1114 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1115 | xmlGenericError(xmlGenericErrorContext, |
| 1116 | "xmlRemoveProp : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1117 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1118 | return(-1); |
| 1119 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1120 | if (cur->parent == NULL) { |
| 1121 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1122 | xmlGenericError(xmlGenericErrorContext, |
| 1123 | "xmlRemoveProp : cur->parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1124 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1125 | return(-1); |
| 1126 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1127 | tmp = cur->parent->properties; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1128 | if (tmp == cur) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1129 | cur->parent->properties = cur->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1130 | xmlFreeProp(cur); |
| 1131 | return(0); |
| 1132 | } |
| 1133 | while (tmp != NULL) { |
| 1134 | if (tmp->next == cur) { |
| 1135 | tmp->next = cur->next; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1136 | if (tmp->next != NULL) |
| 1137 | tmp->next->prev = tmp; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1138 | xmlFreeProp(cur); |
| 1139 | return(0); |
| 1140 | } |
| 1141 | tmp = tmp->next; |
| 1142 | } |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1143 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1144 | xmlGenericError(xmlGenericErrorContext, |
| 1145 | "xmlRemoveProp : attribute not owned by its node\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1146 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1147 | return(-1); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1151 | * xmlNewPI: |
| 1152 | * @name: the processing instruction name |
| 1153 | * @content: the PI content |
| 1154 | * |
| 1155 | * Creation of a processing instruction element. |
| 1156 | * Returns a pointer to the new node object. |
| 1157 | */ |
| 1158 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1159 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1160 | xmlNodePtr cur; |
| 1161 | |
| 1162 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1163 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1164 | xmlGenericError(xmlGenericErrorContext, |
| 1165 | "xmlNewPI : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1166 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1167 | return(NULL); |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Allocate a new node and fill the fields. |
| 1172 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1173 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1174 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1175 | xmlGenericError(xmlGenericErrorContext, |
| 1176 | "xmlNewPI : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1177 | return(NULL); |
| 1178 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1179 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1180 | cur->type = XML_PI_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1181 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1182 | cur->name = xmlStrdup(name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1183 | if (content != NULL) { |
| 1184 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1185 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1186 | #else |
| 1187 | cur->content = xmlBufferCreateSize(0); |
| 1188 | xmlBufferSetAllocationScheme(cur->content, |
| 1189 | xmlGetBufferAllocationScheme()); |
| 1190 | xmlBufferAdd(cur->content, content, -1); |
| 1191 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1192 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1193 | return(cur); |
| 1194 | } |
| 1195 | |
| 1196 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1197 | * xmlNewNode: |
| 1198 | * @ns: namespace if any |
| 1199 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1200 | * |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 1201 | * Creation of a new node element. @ns is optionnal (NULL). |
| 1202 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1203 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1204 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1205 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1206 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1207 | xmlNodePtr cur; |
| 1208 | |
| 1209 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1210 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1211 | xmlGenericError(xmlGenericErrorContext, |
| 1212 | "xmlNewNode : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1213 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1214 | return(NULL); |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Allocate a new node and fill the fields. |
| 1219 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1220 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1221 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1222 | xmlGenericError(xmlGenericErrorContext, |
| 1223 | "xmlNewNode : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1224 | return(NULL); |
| 1225 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1226 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1227 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1228 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1229 | cur->name = xmlStrdup(name); |
| 1230 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1231 | return(cur); |
| 1232 | } |
| 1233 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1234 | /** |
| 1235 | * xmlNewDocNode: |
| 1236 | * @doc: the document |
| 1237 | * @ns: namespace if any |
| 1238 | * @name: the node name |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1239 | * @content: the XML text content if any |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1240 | * |
| 1241 | * Creation of a new node element within a document. @ns and @content |
| 1242 | * are optionnal (NULL). |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1243 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1244 | * references, but XML special chars need to be escaped first by using |
| 1245 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1246 | * need entities support. |
| 1247 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1248 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1249 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1250 | xmlNodePtr |
| 1251 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1252 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1253 | xmlNodePtr cur; |
| 1254 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1255 | cur = xmlNewNode(ns, name); |
| 1256 | if (cur != NULL) { |
| 1257 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1258 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1259 | cur->children = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1260 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1261 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1262 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1263 | return(cur); |
| 1264 | } |
| 1265 | |
| 1266 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1267 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1268 | * xmlNewDocRawNode: |
| 1269 | * @doc: the document |
| 1270 | * @ns: namespace if any |
| 1271 | * @name: the node name |
| 1272 | * @content: the text content if any |
| 1273 | * |
| 1274 | * Creation of a new node element within a document. @ns and @content |
| 1275 | * are optionnal (NULL). |
| 1276 | * |
| 1277 | * Returns a pointer to the new node object. |
| 1278 | */ |
| 1279 | xmlNodePtr |
| 1280 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1281 | const xmlChar *name, const xmlChar *content) { |
| 1282 | xmlNodePtr cur; |
| 1283 | |
| 1284 | cur = xmlNewNode(ns, name); |
| 1285 | if (cur != NULL) { |
| 1286 | cur->doc = doc; |
| 1287 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1288 | cur->children = xmlNewDocText(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1289 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | return(cur); |
| 1293 | } |
| 1294 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1295 | /** |
| 1296 | * xmlNewDocFragment: |
| 1297 | * @doc: the document owning the fragment |
| 1298 | * |
| 1299 | * Creation of a new Fragment node. |
| 1300 | * Returns a pointer to the new node object. |
| 1301 | */ |
| 1302 | xmlNodePtr |
| 1303 | xmlNewDocFragment(xmlDocPtr doc) { |
| 1304 | xmlNodePtr cur; |
| 1305 | |
| 1306 | /* |
| 1307 | * Allocate a new DocumentFragment node and fill the fields. |
| 1308 | */ |
| 1309 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1310 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1311 | xmlGenericError(xmlGenericErrorContext, |
| 1312 | "xmlNewDocFragment : malloc failed\n"); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1313 | return(NULL); |
| 1314 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1315 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1316 | cur->type = XML_DOCUMENT_FRAG_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1317 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1318 | cur->doc = doc; |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1319 | return(cur); |
| 1320 | } |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1321 | |
| 1322 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1323 | * xmlNewText: |
| 1324 | * @content: the text content |
| 1325 | * |
| 1326 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1327 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1328 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1329 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1330 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1331 | xmlNodePtr cur; |
| 1332 | |
| 1333 | /* |
| 1334 | * Allocate a new node and fill the fields. |
| 1335 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1336 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1337 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1338 | xmlGenericError(xmlGenericErrorContext, |
| 1339 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1340 | return(NULL); |
| 1341 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1342 | memset(cur, 0, sizeof(xmlNode)); |
| 1343 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1344 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1345 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1346 | if (content != NULL) { |
| 1347 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1348 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1349 | #else |
| 1350 | cur->content = xmlBufferCreateSize(0); |
| 1351 | xmlBufferSetAllocationScheme(cur->content, |
| 1352 | xmlGetBufferAllocationScheme()); |
| 1353 | xmlBufferAdd(cur->content, content, -1); |
| 1354 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1355 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1356 | return(cur); |
| 1357 | } |
| 1358 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1359 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1360 | * xmlNewTextChild: |
| 1361 | * @parent: the parent node |
| 1362 | * @ns: a namespace if any |
| 1363 | * @name: the name of the child |
| 1364 | * @content: the text content of the child if any. |
| 1365 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1366 | * 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] | 1367 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1368 | * a child TEXT node will be created containing the string content. |
| 1369 | * |
| 1370 | * Returns a pointer to the new node object. |
| 1371 | */ |
| 1372 | xmlNodePtr |
| 1373 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1374 | const xmlChar *name, const xmlChar *content) { |
| 1375 | xmlNodePtr cur, prev; |
| 1376 | |
| 1377 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1378 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1379 | xmlGenericError(xmlGenericErrorContext, |
| 1380 | "xmlNewTextChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1381 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1382 | return(NULL); |
| 1383 | } |
| 1384 | |
| 1385 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1386 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1387 | xmlGenericError(xmlGenericErrorContext, |
| 1388 | "xmlNewTextChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1389 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1390 | return(NULL); |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * Allocate a new node |
| 1395 | */ |
| 1396 | if (ns == NULL) |
| 1397 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1398 | else |
| 1399 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1400 | if (cur == NULL) return(NULL); |
| 1401 | |
| 1402 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1403 | * add the new element at the end of the children list. |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1404 | */ |
| 1405 | cur->type = XML_ELEMENT_NODE; |
| 1406 | cur->parent = parent; |
| 1407 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1408 | if (parent->children == NULL) { |
| 1409 | parent->children = cur; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1410 | parent->last = cur; |
| 1411 | } else { |
| 1412 | prev = parent->last; |
| 1413 | prev->next = cur; |
| 1414 | cur->prev = prev; |
| 1415 | parent->last = cur; |
| 1416 | } |
| 1417 | |
| 1418 | return(cur); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1422 | * xmlNewCharRef: |
| 1423 | * @doc: the document |
| 1424 | * @name: the char ref string, starting with # or "&# ... ;" |
| 1425 | * |
| 1426 | * Creation of a new character reference node. |
| 1427 | * Returns a pointer to the new node object. |
| 1428 | */ |
| 1429 | xmlNodePtr |
| 1430 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 1431 | xmlNodePtr cur; |
| 1432 | |
| 1433 | /* |
| 1434 | * Allocate a new node and fill the fields. |
| 1435 | */ |
| 1436 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1437 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1438 | xmlGenericError(xmlGenericErrorContext, |
| 1439 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1440 | return(NULL); |
| 1441 | } |
| 1442 | memset(cur, 0, sizeof(xmlNode)); |
| 1443 | cur->type = XML_ENTITY_REF_NODE; |
| 1444 | |
| 1445 | cur->doc = doc; |
| 1446 | if (name[0] == '&') { |
| 1447 | int len; |
| 1448 | name++; |
| 1449 | len = xmlStrlen(name); |
| 1450 | if (name[len - 1] == ';') |
| 1451 | cur->name = xmlStrndup(name, len - 1); |
| 1452 | else |
| 1453 | cur->name = xmlStrndup(name, len); |
| 1454 | } else |
| 1455 | cur->name = xmlStrdup(name); |
| 1456 | return(cur); |
| 1457 | } |
| 1458 | |
| 1459 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1460 | * xmlNewReference: |
| 1461 | * @doc: the document |
| 1462 | * @name: the reference name, or the reference string with & and ; |
| 1463 | * |
| 1464 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1465 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1466 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1467 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1468 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1469 | xmlNodePtr cur; |
| 1470 | xmlEntityPtr ent; |
| 1471 | |
| 1472 | /* |
| 1473 | * Allocate a new node and fill the fields. |
| 1474 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1475 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1476 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1477 | xmlGenericError(xmlGenericErrorContext, |
| 1478 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1479 | return(NULL); |
| 1480 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1481 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1482 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1483 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1484 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1485 | if (name[0] == '&') { |
| 1486 | int len; |
| 1487 | name++; |
| 1488 | len = xmlStrlen(name); |
| 1489 | if (name[len - 1] == ';') |
| 1490 | cur->name = xmlStrndup(name, len - 1); |
| 1491 | else |
| 1492 | cur->name = xmlStrndup(name, len); |
| 1493 | } else |
| 1494 | cur->name = xmlStrdup(name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1495 | |
| 1496 | ent = xmlGetDocEntity(doc, cur->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1497 | if (ent != NULL) { |
| 1498 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1499 | cur->content = ent->content; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1500 | #else |
| 1501 | /* |
| 1502 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1503 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1504 | * later |
| 1505 | */ |
| 1506 | cur->content = xmlBufferCreateSize(0); |
| 1507 | xmlBufferSetAllocationScheme(cur->content, |
| 1508 | xmlGetBufferAllocationScheme()); |
| 1509 | if (ent->content != NULL) |
| 1510 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1511 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1512 | /* |
| 1513 | * The parent pointer in entity is a Dtd pointer and thus is NOT |
| 1514 | * updated. Not sure if this is 100% correct. |
| 1515 | * -George |
| 1516 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1517 | cur->children = (xmlNodePtr) ent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1518 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1519 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1520 | return(cur); |
| 1521 | } |
| 1522 | |
| 1523 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1524 | * xmlNewDocText: |
| 1525 | * @doc: the document |
| 1526 | * @content: the text content |
| 1527 | * |
| 1528 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1529 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1530 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1531 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1532 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1533 | xmlNodePtr cur; |
| 1534 | |
| 1535 | cur = xmlNewText(content); |
| 1536 | if (cur != NULL) cur->doc = doc; |
| 1537 | return(cur); |
| 1538 | } |
| 1539 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1540 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1541 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1542 | * @content: the text content |
| 1543 | * @len: the text len. |
| 1544 | * |
| 1545 | * 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] | 1546 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1547 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1548 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1549 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1550 | xmlNodePtr cur; |
| 1551 | |
| 1552 | /* |
| 1553 | * Allocate a new node and fill the fields. |
| 1554 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1555 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1556 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1557 | xmlGenericError(xmlGenericErrorContext, |
| 1558 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1559 | return(NULL); |
| 1560 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1561 | memset(cur, 0, sizeof(xmlNode)); |
| 1562 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1563 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1564 | cur->name = xmlStrdup(xmlStringText); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1565 | if (content != NULL) { |
| 1566 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1567 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1568 | #else |
| 1569 | cur->content = xmlBufferCreateSize(len); |
| 1570 | xmlBufferSetAllocationScheme(cur->content, |
| 1571 | xmlGetBufferAllocationScheme()); |
| 1572 | xmlBufferAdd(cur->content, content, len); |
| 1573 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1574 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1575 | return(cur); |
| 1576 | } |
| 1577 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1578 | /** |
| 1579 | * xmlNewDocTextLen: |
| 1580 | * @doc: the document |
| 1581 | * @content: the text content |
| 1582 | * @len: the text len. |
| 1583 | * |
| 1584 | * Creation of a new text node with an extra content lenght parameter. The |
| 1585 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1586 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1587 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1588 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1589 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1590 | xmlNodePtr cur; |
| 1591 | |
| 1592 | cur = xmlNewTextLen(content, len); |
| 1593 | if (cur != NULL) cur->doc = doc; |
| 1594 | return(cur); |
| 1595 | } |
| 1596 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1597 | /** |
| 1598 | * xmlNewComment: |
| 1599 | * @content: the comment content |
| 1600 | * |
| 1601 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1602 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1603 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1604 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1605 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1606 | xmlNodePtr cur; |
| 1607 | |
| 1608 | /* |
| 1609 | * Allocate a new node and fill the fields. |
| 1610 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1611 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1612 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1613 | xmlGenericError(xmlGenericErrorContext, |
| 1614 | "xmlNewComment : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1615 | return(NULL); |
| 1616 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1617 | memset(cur, 0, sizeof(xmlNode)); |
| 1618 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1619 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1620 | cur->name = xmlStrdup(xmlStringComment); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1621 | if (content != NULL) { |
| 1622 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1623 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1624 | #else |
| 1625 | cur->content = xmlBufferCreateSize(0); |
| 1626 | xmlBufferSetAllocationScheme(cur->content, |
| 1627 | xmlGetBufferAllocationScheme()); |
| 1628 | xmlBufferAdd(cur->content, content, -1); |
| 1629 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1630 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1631 | return(cur); |
| 1632 | } |
| 1633 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1634 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1635 | * xmlNewCDataBlock: |
| 1636 | * @doc: the document |
| 1637 | * @content: the CData block content content |
| 1638 | * @len: the length of the block |
| 1639 | * |
| 1640 | * Creation of a new node containing a CData block. |
| 1641 | * Returns a pointer to the new node object. |
| 1642 | */ |
| 1643 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1644 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1645 | xmlNodePtr cur; |
| 1646 | |
| 1647 | /* |
| 1648 | * Allocate a new node and fill the fields. |
| 1649 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1650 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1651 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1652 | xmlGenericError(xmlGenericErrorContext, |
| 1653 | "xmlNewCDataBlock : malloc failed\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1654 | return(NULL); |
| 1655 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1656 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1657 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1658 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1659 | if (content != NULL) { |
| 1660 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1661 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1662 | #else |
| 1663 | cur->content = xmlBufferCreateSize(len); |
| 1664 | xmlBufferSetAllocationScheme(cur->content, |
| 1665 | xmlGetBufferAllocationScheme()); |
| 1666 | xmlBufferAdd(cur->content, content, len); |
| 1667 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1668 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1669 | return(cur); |
| 1670 | } |
| 1671 | |
| 1672 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1673 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1674 | * @doc: the document |
| 1675 | * @content: the comment content |
| 1676 | * |
| 1677 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1678 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1679 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1680 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1681 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1682 | xmlNodePtr cur; |
| 1683 | |
| 1684 | cur = xmlNewComment(content); |
| 1685 | if (cur != NULL) cur->doc = doc; |
| 1686 | return(cur); |
| 1687 | } |
| 1688 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1689 | /** |
| 1690 | * xmlSetTreeDoc: |
| 1691 | * @tree: the top element |
| 1692 | * @doc: the document |
| 1693 | * |
| 1694 | * update all nodes under the tree to point to the right document |
| 1695 | */ |
| 1696 | void |
| 1697 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
| 1698 | if (tree == NULL) |
| 1699 | return; |
| 1700 | if (tree->type == XML_ENTITY_DECL) |
| 1701 | return; |
| 1702 | if (tree->doc != doc) { |
| 1703 | if (tree->children != NULL) |
| 1704 | xmlSetListDoc(tree->children, doc); |
| 1705 | tree->doc = doc; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * xmlSetListDoc: |
| 1711 | * @tree: the first element |
| 1712 | * @doc: the document |
| 1713 | * |
| 1714 | * update all nodes in the list to point to the right document |
| 1715 | */ |
| 1716 | void |
| 1717 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 1718 | xmlNodePtr cur; |
| 1719 | |
| 1720 | if (list == NULL) |
| 1721 | return; |
| 1722 | cur = list; |
| 1723 | while (cur != NULL) { |
| 1724 | if (cur->doc != doc) |
| 1725 | xmlSetTreeDoc(cur, doc); |
| 1726 | cur = cur->next; |
| 1727 | } |
| 1728 | } |
| 1729 | |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1730 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1731 | /** |
| 1732 | * xmlNewChild: |
| 1733 | * @parent: the parent node |
| 1734 | * @ns: a namespace if any |
| 1735 | * @name: the name of the child |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1736 | * @content: the XML content of the child if any. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1737 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1738 | * 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] | 1739 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1740 | * 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] | 1741 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1742 | * references, but XML special chars need to be escaped first by using |
| 1743 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1744 | * support is not needed. |
| 1745 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1746 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1747 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1748 | xmlNodePtr |
| 1749 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1750 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1751 | xmlNodePtr cur, prev; |
| 1752 | |
| 1753 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1754 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1755 | xmlGenericError(xmlGenericErrorContext, |
| 1756 | "xmlNewChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1757 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1758 | return(NULL); |
| 1759 | } |
| 1760 | |
| 1761 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1762 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1763 | xmlGenericError(xmlGenericErrorContext, |
| 1764 | "xmlNewChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1765 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1766 | return(NULL); |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Allocate a new node |
| 1771 | */ |
| 1772 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1773 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1774 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1775 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1776 | if (cur == NULL) return(NULL); |
| 1777 | |
| 1778 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1779 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1780 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1781 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1782 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1783 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1784 | if (parent->children == NULL) { |
| 1785 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1786 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1787 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1788 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1789 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1790 | cur->prev = prev; |
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 | } |
| 1793 | |
| 1794 | return(cur); |
| 1795 | } |
| 1796 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1797 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1798 | * xmlAddNextSibling: |
| 1799 | * @cur: the child node |
| 1800 | * @elem: the new node |
| 1801 | * |
| 1802 | * Add a new element @elem as the next siblings of @cur |
| 1803 | * If the new element was already inserted in a document it is |
| 1804 | * first unlinked from its existing context. |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1805 | * As a result of text merging @elem may be freed. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1806 | * |
| 1807 | * Returns the new element or NULL in case of error. |
| 1808 | */ |
| 1809 | xmlNodePtr |
| 1810 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1811 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1812 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1813 | xmlGenericError(xmlGenericErrorContext, |
| 1814 | "xmlAddNextSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1815 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1816 | return(NULL); |
| 1817 | } |
| 1818 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1819 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1820 | xmlGenericError(xmlGenericErrorContext, |
| 1821 | "xmlAddNextSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1822 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1823 | return(NULL); |
| 1824 | } |
| 1825 | |
| 1826 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1827 | |
| 1828 | if (elem->type == XML_TEXT_NODE) { |
| 1829 | if (cur->type == XML_TEXT_NODE) { |
| 1830 | #ifndef XML_USE_BUFFER_CONTENT |
| 1831 | xmlNodeAddContent(cur, elem->content); |
| 1832 | #else |
| 1833 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1834 | #endif |
| 1835 | xmlFreeNode(elem); |
| 1836 | return(cur); |
| 1837 | } |
| 1838 | if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) { |
| 1839 | #ifndef XML_USE_BUFFER_CONTENT |
| 1840 | xmlChar *tmp; |
| 1841 | |
| 1842 | tmp = xmlStrdup(elem->content); |
| 1843 | tmp = xmlStrcat(tmp, cur->next->content); |
| 1844 | xmlNodeSetContent(cur->next, tmp); |
| 1845 | xmlFree(tmp); |
| 1846 | #else |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1847 | xmlBufferAddHead(cur->next->content, |
| 1848 | xmlBufferContent(elem->content), |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1849 | xmlBufferLength(elem->content)); |
| 1850 | #endif |
| 1851 | xmlFreeNode(elem); |
| 1852 | return(cur->next); |
| 1853 | } |
| 1854 | } |
| 1855 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1856 | if (elem->doc != cur->doc) { |
| 1857 | xmlSetTreeDoc(elem, cur->doc); |
| 1858 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1859 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1860 | elem->prev = cur; |
| 1861 | elem->next = cur->next; |
| 1862 | cur->next = elem; |
| 1863 | if (elem->next != NULL) |
| 1864 | elem->next->prev = elem; |
| 1865 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1866 | elem->parent->last = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1867 | return(elem); |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * xmlAddPrevSibling: |
| 1872 | * @cur: the child node |
| 1873 | * @elem: the new node |
| 1874 | * |
| 1875 | * Add a new element @elem as the previous siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1876 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1877 | * If the new element was already inserted in a document it is |
| 1878 | * first unlinked from its existing context. |
| 1879 | * |
| 1880 | * Returns the new element or NULL in case of error. |
| 1881 | */ |
| 1882 | xmlNodePtr |
| 1883 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1884 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1885 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1886 | xmlGenericError(xmlGenericErrorContext, |
| 1887 | "xmlAddPrevSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1888 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1889 | return(NULL); |
| 1890 | } |
| 1891 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1892 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1893 | xmlGenericError(xmlGenericErrorContext, |
| 1894 | "xmlAddPrevSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1895 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1896 | return(NULL); |
| 1897 | } |
| 1898 | |
| 1899 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1900 | |
| 1901 | if (elem->type == XML_TEXT_NODE) { |
| 1902 | if (cur->type == XML_TEXT_NODE) { |
| 1903 | #ifndef XML_USE_BUFFER_CONTENT |
| 1904 | xmlChar *tmp; |
| 1905 | |
| 1906 | tmp = xmlStrdup(elem->content); |
| 1907 | tmp = xmlStrcat(tmp, cur->content); |
| 1908 | xmlNodeSetContent(cur, tmp); |
| 1909 | xmlFree(tmp); |
| 1910 | #else |
| 1911 | xmlBufferAddHead(cur->content, xmlBufferContent(elem->content), |
| 1912 | xmlBufferLength(elem->content)); |
| 1913 | #endif |
| 1914 | xmlFreeNode(elem); |
| 1915 | return(cur); |
| 1916 | } |
| 1917 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) { |
| 1918 | #ifndef XML_USE_BUFFER_CONTENT |
| 1919 | xmlNodeAddContent(cur->prev, elem->content); |
| 1920 | #else |
| 1921 | xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content)); |
| 1922 | #endif |
| 1923 | xmlFreeNode(elem); |
| 1924 | return(cur->prev); |
| 1925 | } |
| 1926 | } |
| 1927 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1928 | if (elem->doc != cur->doc) { |
| 1929 | xmlSetTreeDoc(elem, cur->doc); |
| 1930 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1931 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1932 | elem->next = cur; |
| 1933 | elem->prev = cur->prev; |
| 1934 | cur->prev = elem; |
| 1935 | if (elem->prev != NULL) |
| 1936 | elem->prev->next = elem; |
| 1937 | if ((elem->parent != NULL) && (elem->parent->children == cur)) |
| 1938 | elem->parent->children = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1939 | return(elem); |
| 1940 | } |
| 1941 | |
| 1942 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1943 | * xmlAddSibling: |
| 1944 | * @cur: the child node |
| 1945 | * @elem: the new node |
| 1946 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1947 | * Add a new element @elem to the list of siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1948 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1949 | * If the new element was already inserted in a document it is |
| 1950 | * first unlinked from its existing context. |
| 1951 | * |
| 1952 | * Returns the new element or NULL in case of error. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1953 | */ |
| 1954 | xmlNodePtr |
| 1955 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1956 | xmlNodePtr parent; |
| 1957 | |
| 1958 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1959 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1960 | xmlGenericError(xmlGenericErrorContext, |
| 1961 | "xmlAddSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1962 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1963 | return(NULL); |
| 1964 | } |
| 1965 | |
| 1966 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1967 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1968 | xmlGenericError(xmlGenericErrorContext, |
| 1969 | "xmlAddSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1970 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1971 | return(NULL); |
| 1972 | } |
| 1973 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1974 | /* |
| 1975 | * Constant time is we can rely on the ->parent->last to find |
| 1976 | * the last sibling. |
| 1977 | */ |
| 1978 | if ((cur->parent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1979 | (cur->parent->children != NULL) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1980 | (cur->parent->last != NULL) && |
| 1981 | (cur->parent->last->next == NULL)) { |
| 1982 | cur = cur->parent->last; |
| 1983 | } else { |
| 1984 | while (cur->next != NULL) cur = cur->next; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1987 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1988 | |
| 1989 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
| 1990 | #ifndef XML_USE_BUFFER_CONTENT |
| 1991 | xmlNodeAddContent(cur, elem->content); |
| 1992 | #else |
| 1993 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1994 | #endif |
| 1995 | xmlFreeNode(elem); |
| 1996 | return(cur); |
| 1997 | } |
| 1998 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1999 | if (elem->doc != cur->doc) { |
| 2000 | xmlSetTreeDoc(elem, cur->doc); |
| 2001 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2002 | parent = cur->parent; |
| 2003 | elem->prev = cur; |
| 2004 | elem->next = NULL; |
| 2005 | elem->parent = parent; |
| 2006 | cur->next = elem; |
| 2007 | if (parent != NULL) |
| 2008 | parent->last = elem; |
| 2009 | |
| 2010 | return(elem); |
| 2011 | } |
| 2012 | |
| 2013 | /** |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2014 | * xmlAddChildList: |
| 2015 | * @parent: the parent node |
| 2016 | * @cur: the first node in the list |
| 2017 | * |
| 2018 | * 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] | 2019 | * merging adjacent TEXT nodes (@cur may be freed) |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2020 | * |
| 2021 | * Returns the last child or NULL in case of error. |
| 2022 | */ |
| 2023 | xmlNodePtr |
| 2024 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2025 | xmlNodePtr prev; |
| 2026 | |
| 2027 | if (parent == NULL) { |
| 2028 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2029 | xmlGenericError(xmlGenericErrorContext, |
| 2030 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2031 | #endif |
| 2032 | return(NULL); |
| 2033 | } |
| 2034 | |
| 2035 | if (cur == NULL) { |
| 2036 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2037 | xmlGenericError(xmlGenericErrorContext, |
| 2038 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2039 | #endif |
| 2040 | return(NULL); |
| 2041 | } |
| 2042 | |
| 2043 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2044 | (cur->doc != parent->doc)) { |
| 2045 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2046 | xmlGenericError(xmlGenericErrorContext, |
| 2047 | "Elements moved to a different document\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2048 | #endif |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * add the first element at the end of the children list. |
| 2053 | */ |
| 2054 | if (parent->children == NULL) { |
| 2055 | parent->children = cur; |
| 2056 | } else { |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2057 | /* |
| 2058 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 2059 | */ |
| 2060 | if ((cur->type == XML_TEXT_NODE) && |
| 2061 | (parent->last->type == XML_TEXT_NODE)) { |
| 2062 | #ifndef XML_USE_BUFFER_CONTENT |
| 2063 | xmlNodeAddContent(parent->last, cur->content); |
| 2064 | #else |
| 2065 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2066 | #endif |
| 2067 | /* |
| 2068 | * if it's the only child, nothing more to be done. |
| 2069 | */ |
| 2070 | if (cur->next == NULL) { |
| 2071 | xmlFreeNode(cur); |
| 2072 | return(parent->last); |
| 2073 | } |
| 2074 | prev = cur; |
| 2075 | cur = cur->next; |
| 2076 | xmlFreeNode(prev); |
| 2077 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2078 | prev = parent->last; |
| 2079 | prev->next = cur; |
| 2080 | cur->prev = prev; |
| 2081 | } |
| 2082 | while (cur->next != NULL) { |
| 2083 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2084 | if (cur->doc != parent->doc) { |
| 2085 | xmlSetTreeDoc(cur, parent->doc); |
| 2086 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2087 | cur = cur->next; |
| 2088 | } |
| 2089 | cur->parent = parent; |
| 2090 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 2091 | parent->last = cur; |
| 2092 | |
| 2093 | return(cur); |
| 2094 | } |
| 2095 | |
| 2096 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2097 | * xmlAddChild: |
| 2098 | * @parent: the parent node |
| 2099 | * @cur: the child node |
| 2100 | * |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2101 | * Add a new child element, to @parent, at the end of the child list |
| 2102 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2103 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2104 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2105 | xmlNodePtr |
| 2106 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2107 | xmlNodePtr prev; |
| 2108 | |
| 2109 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2110 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2111 | xmlGenericError(xmlGenericErrorContext, |
| 2112 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2113 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2114 | return(NULL); |
| 2115 | } |
| 2116 | |
| 2117 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2118 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2119 | xmlGenericError(xmlGenericErrorContext, |
| 2120 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2121 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2122 | return(NULL); |
| 2123 | } |
| 2124 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2125 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2126 | (cur->doc != parent->doc)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2127 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2128 | xmlGenericError(xmlGenericErrorContext, |
| 2129 | "Elements moved to a different document\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2130 | #endif |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2133 | /* |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2134 | * If cur is a TEXT node, merge its content with adjacent TEXT nodes |
| 2135 | * or with parent->content if parent->content != NULL. |
| 2136 | * cur is then freed. |
| 2137 | */ |
| 2138 | if (cur->type == XML_TEXT_NODE) { |
| 2139 | if (((parent->type == XML_ELEMENT_NODE) || |
| 2140 | (parent->type == XML_TEXT_NODE)) && |
| 2141 | (parent->content != NULL)) { |
| 2142 | #ifndef XML_USE_BUFFER_CONTENT |
| 2143 | xmlNodeAddContent(parent, cur->content); |
| 2144 | #else |
| 2145 | xmlNodeAddContent(parent, xmlBufferContent(cur->content)); |
| 2146 | #endif |
| 2147 | xmlFreeNode(cur); |
| 2148 | return(parent); |
| 2149 | } |
| 2150 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE)) { |
| 2151 | #ifndef XML_USE_BUFFER_CONTENT |
| 2152 | xmlNodeAddContent(parent->last, cur->content); |
| 2153 | #else |
| 2154 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2155 | #endif |
| 2156 | xmlFreeNode(cur); |
| 2157 | return(parent->last); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2162 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2163 | */ |
| 2164 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2165 | if (cur->doc != parent->doc) { |
| 2166 | xmlSetTreeDoc(cur, parent->doc); |
| 2167 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2168 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2169 | /* |
| 2170 | * Handle the case where parent->content != NULL, in that case it will |
| 2171 | * create a intermediate TEXT node. |
| 2172 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2173 | if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && |
| 2174 | (parent->content != NULL)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2175 | xmlNodePtr text; |
| 2176 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2177 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2178 | text = xmlNewDocText(parent->doc, parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2179 | #else |
| 2180 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 2181 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2182 | if (text != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2183 | text->next = parent->children; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2184 | if (text->next != NULL) |
| 2185 | text->next->prev = text; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2186 | parent->children = text; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2187 | UPDATE_LAST_CHILD_AND_PARENT(parent) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2188 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2189 | xmlFree(parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2190 | #else |
| 2191 | xmlBufferFree(parent->content); |
| 2192 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2193 | parent->content = NULL; |
| 2194 | } |
| 2195 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2196 | if (parent->children == NULL) { |
| 2197 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2198 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2199 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2200 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2201 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2202 | cur->prev = prev; |
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 | } |
| 2205 | |
| 2206 | return(cur); |
| 2207 | } |
| 2208 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2209 | /** |
| 2210 | * xmlGetLastChild: |
| 2211 | * @parent: the parent node |
| 2212 | * |
| 2213 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2214 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2215 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2216 | xmlNodePtr |
| 2217 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2218 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2219 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2220 | xmlGenericError(xmlGenericErrorContext, |
| 2221 | "xmlGetLastChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2222 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2223 | return(NULL); |
| 2224 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2225 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2228 | /** |
| 2229 | * xmlFreeNodeList: |
| 2230 | * @cur: the first node in the list |
| 2231 | * |
| 2232 | * 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] | 2233 | * the children are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2234 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2235 | void |
| 2236 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2237 | xmlNodePtr next; |
| 2238 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2239 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2240 | xmlGenericError(xmlGenericErrorContext, |
| 2241 | "xmlFreeNodeList : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2242 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2243 | return; |
| 2244 | } |
| 2245 | while (cur != NULL) { |
| 2246 | next = cur->next; |
| 2247 | xmlFreeNode(cur); |
| 2248 | cur = next; |
| 2249 | } |
| 2250 | } |
| 2251 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2252 | /** |
| 2253 | * xmlFreeNode: |
| 2254 | * @cur: the node |
| 2255 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2256 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 2257 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2258 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2259 | void |
| 2260 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2261 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2262 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2263 | xmlGenericError(xmlGenericErrorContext, |
| 2264 | "xmlFreeNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2265 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2266 | return; |
| 2267 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2268 | if (cur->type == XML_DTD_NODE) |
| 2269 | return; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2270 | cur->doc = NULL; |
| 2271 | cur->parent = NULL; |
| 2272 | cur->next = NULL; |
| 2273 | cur->prev = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2274 | if ((cur->children != NULL) && |
| 2275 | (cur->type != XML_ENTITY_REF_NODE)) |
| 2276 | xmlFreeNodeList(cur->children); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2277 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 2278 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2279 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2280 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2281 | #else |
| 2282 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 2283 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2284 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2285 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 2286 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2287 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2290 | /** |
| 2291 | * xmlUnlinkNode: |
| 2292 | * @cur: the node |
| 2293 | * |
| 2294 | * Unlink a node from it's current context, the node is not freed |
| 2295 | */ |
| 2296 | void |
| 2297 | xmlUnlinkNode(xmlNodePtr cur) { |
| 2298 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2299 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2300 | xmlGenericError(xmlGenericErrorContext, |
| 2301 | "xmlUnlinkNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2302 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2303 | return; |
| 2304 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2305 | if ((cur->parent != NULL) && (cur->parent->children == cur)) |
| 2306 | cur->parent->children = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2307 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 2308 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2309 | if (cur->next != NULL) |
| 2310 | cur->next->prev = cur->prev; |
| 2311 | if (cur->prev != NULL) |
| 2312 | cur->prev->next = cur->next; |
| 2313 | cur->next = cur->prev = NULL; |
| 2314 | cur->parent = NULL; |
| 2315 | } |
| 2316 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2317 | /** |
| 2318 | * xmlReplaceNode: |
| 2319 | * @old: the old node |
| 2320 | * @cur: the node |
| 2321 | * |
| 2322 | * Unlink the old node from it's current context, prune the new one |
| 2323 | * at the same place. If cur was already inserted in a document it is |
| 2324 | * first unlinked from its existing context. |
| 2325 | * |
| 2326 | * Returns the old node |
| 2327 | */ |
| 2328 | xmlNodePtr |
| 2329 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 2330 | if (old == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2331 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2332 | xmlGenericError(xmlGenericErrorContext, |
| 2333 | "xmlReplaceNode : old == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2334 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2335 | return(NULL); |
| 2336 | } |
| 2337 | if (cur == NULL) { |
| 2338 | xmlUnlinkNode(old); |
| 2339 | return(old); |
| 2340 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2341 | if (cur == old) { |
| 2342 | return(old); |
| 2343 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2344 | xmlUnlinkNode(cur); |
| 2345 | cur->doc = old->doc; |
| 2346 | cur->parent = old->parent; |
| 2347 | cur->next = old->next; |
| 2348 | if (cur->next != NULL) |
| 2349 | cur->next->prev = cur; |
| 2350 | cur->prev = old->prev; |
| 2351 | if (cur->prev != NULL) |
| 2352 | cur->prev->next = cur; |
| 2353 | if (cur->parent != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2354 | if (cur->parent->children == old) |
| 2355 | cur->parent->children = cur; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2356 | if (cur->parent->last == old) |
| 2357 | cur->parent->last = cur; |
| 2358 | } |
| 2359 | old->next = old->prev = NULL; |
| 2360 | old->parent = NULL; |
| 2361 | return(old); |
| 2362 | } |
| 2363 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2364 | /************************************************************************ |
| 2365 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2366 | * Copy operations * |
| 2367 | * * |
| 2368 | ************************************************************************/ |
| 2369 | |
| 2370 | /** |
| 2371 | * xmlCopyNamespace: |
| 2372 | * @cur: the namespace |
| 2373 | * |
| 2374 | * Do a copy of the namespace. |
| 2375 | * |
| 2376 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2377 | */ |
| 2378 | xmlNsPtr |
| 2379 | xmlCopyNamespace(xmlNsPtr cur) { |
| 2380 | xmlNsPtr ret; |
| 2381 | |
| 2382 | if (cur == NULL) return(NULL); |
| 2383 | switch (cur->type) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2384 | case XML_LOCAL_NAMESPACE: |
| 2385 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 2386 | break; |
| 2387 | default: |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2388 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2389 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2390 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2391 | #endif |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2392 | return(NULL); |
| 2393 | } |
| 2394 | return(ret); |
| 2395 | } |
| 2396 | |
| 2397 | /** |
| 2398 | * xmlCopyNamespaceList: |
| 2399 | * @cur: the first namespace |
| 2400 | * |
| 2401 | * Do a copy of an namespace list. |
| 2402 | * |
| 2403 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2404 | */ |
| 2405 | xmlNsPtr |
| 2406 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 2407 | xmlNsPtr ret = NULL; |
| 2408 | xmlNsPtr p = NULL,q; |
| 2409 | |
| 2410 | while (cur != NULL) { |
| 2411 | q = xmlCopyNamespace(cur); |
| 2412 | if (p == NULL) { |
| 2413 | ret = p = q; |
| 2414 | } else { |
| 2415 | p->next = q; |
| 2416 | p = q; |
| 2417 | } |
| 2418 | cur = cur->next; |
| 2419 | } |
| 2420 | return(ret); |
| 2421 | } |
| 2422 | |
| 2423 | /** |
| 2424 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2425 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2426 | * @cur: the attribute |
| 2427 | * |
| 2428 | * Do a copy of the attribute. |
| 2429 | * |
| 2430 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2431 | */ |
| 2432 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2433 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2434 | xmlAttrPtr ret; |
| 2435 | |
| 2436 | if (cur == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2437 | if (cur->parent != NULL) |
| 2438 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 2439 | else if (cur->children != NULL) |
| 2440 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2441 | else |
| 2442 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2443 | if (ret == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2444 | ret->parent = target; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2445 | |
| 2446 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2447 | xmlNsPtr ns; |
| 2448 | |
| 2449 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2450 | ret->ns = ns; |
| 2451 | } else |
| 2452 | ret->ns = NULL; |
| 2453 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2454 | if (cur->children != NULL) { |
| 2455 | xmlNodePtr tmp; |
| 2456 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2457 | ret->children = xmlCopyNodeList(cur->children); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2458 | ret->last = NULL; |
| 2459 | tmp = ret->children; |
| 2460 | while (tmp != NULL) { |
| 2461 | tmp->parent = (xmlNodePtr)ret; |
| 2462 | if (tmp->next == NULL) |
| 2463 | ret->last = tmp; |
| 2464 | tmp = tmp->next; |
| 2465 | } |
| 2466 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2467 | return(ret); |
| 2468 | } |
| 2469 | |
| 2470 | /** |
| 2471 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2472 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2473 | * @cur: the first attribute |
| 2474 | * |
| 2475 | * Do a copy of an attribute list. |
| 2476 | * |
| 2477 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2478 | */ |
| 2479 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2480 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2481 | xmlAttrPtr ret = NULL; |
| 2482 | xmlAttrPtr p = NULL,q; |
| 2483 | |
| 2484 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2485 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2486 | if (p == NULL) { |
| 2487 | ret = p = q; |
| 2488 | } else { |
| 2489 | p->next = q; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2490 | q->prev = p; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2491 | p = q; |
| 2492 | } |
| 2493 | cur = cur->next; |
| 2494 | } |
| 2495 | return(ret); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 2499 | * NOTE abeut the CopyNode operations ! |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2500 | * |
| 2501 | * They are splitted into external and internal parts for one |
| 2502 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2503 | * say RPM:Copyright without changing the namespace pointer to |
| 2504 | * something else can produce stale links. One way to do it is |
| 2505 | * to keep a reference counter but this doesn't work as soon |
| 2506 | * as one move the element or the subtree out of the scope of |
| 2507 | * the existing namespace. The actual solution seems to add |
| 2508 | * a copy of the namespace at the top of the copied tree if |
| 2509 | * not available in the subtree. |
| 2510 | * Hence two functions, the public front-end call the inner ones |
| 2511 | */ |
| 2512 | |
| 2513 | static xmlNodePtr |
| 2514 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2515 | |
| 2516 | static xmlNodePtr |
| 2517 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2518 | int recursive) { |
| 2519 | xmlNodePtr ret; |
| 2520 | |
| 2521 | if (node == NULL) return(NULL); |
| 2522 | /* |
| 2523 | * Allocate a new node and fill the fields. |
| 2524 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2525 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2526 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2527 | xmlGenericError(xmlGenericErrorContext, |
| 2528 | "xmlStaticCopyNode : malloc failed\n"); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2529 | return(NULL); |
| 2530 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2531 | memset(ret, 0, sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2532 | ret->type = node->type; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2533 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2534 | ret->doc = doc; |
| 2535 | ret->parent = parent; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2536 | if (node->name != NULL) |
| 2537 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2538 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2539 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2540 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2541 | #else |
| 2542 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2543 | xmlBufferSetAllocationScheme(ret->content, |
| 2544 | xmlGetBufferAllocationScheme()); |
| 2545 | xmlBufferAdd(ret->content, |
| 2546 | xmlBufferContent(node->content), |
| 2547 | xmlBufferLength(node->content)); |
| 2548 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2549 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2550 | if (parent != NULL) |
| 2551 | xmlAddChild(parent, ret); |
| 2552 | |
| 2553 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2554 | if (node->nsDef != NULL) |
| 2555 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2556 | |
| 2557 | if (node->ns != NULL) { |
| 2558 | xmlNsPtr ns; |
| 2559 | |
| 2560 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2561 | if (ns == NULL) { |
| 2562 | /* |
| 2563 | * Humm, we are copying an element whose namespace is defined |
| 2564 | * out of the new tree scope. Search it in the original tree |
| 2565 | * and add it at the top of the new tree |
| 2566 | */ |
| 2567 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2568 | if (ns != NULL) { |
| 2569 | xmlNodePtr root = ret; |
| 2570 | |
| 2571 | while (root->parent != NULL) root = root->parent; |
| 2572 | xmlNewNs(root, ns->href, ns->prefix); |
| 2573 | } |
| 2574 | } else { |
| 2575 | /* |
| 2576 | * reference the existing namespace definition in our own tree. |
| 2577 | */ |
| 2578 | ret->ns = ns; |
| 2579 | } |
| 2580 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2581 | if (node->properties != NULL) |
| 2582 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2583 | if (node->children != NULL) |
| 2584 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2585 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2586 | return(ret); |
| 2587 | } |
| 2588 | |
| 2589 | static xmlNodePtr |
| 2590 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2591 | xmlNodePtr ret = NULL; |
| 2592 | xmlNodePtr p = NULL,q; |
| 2593 | |
| 2594 | while (node != NULL) { |
| 2595 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2596 | if (ret == NULL) { |
| 2597 | q->prev = NULL; |
| 2598 | ret = p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2599 | } else { |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2600 | p->next = q; |
| 2601 | q->prev = p; |
| 2602 | p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2603 | } |
| 2604 | node = node->next; |
| 2605 | } |
| 2606 | return(ret); |
| 2607 | } |
| 2608 | |
| 2609 | /** |
| 2610 | * xmlCopyNode: |
| 2611 | * @node: the node |
| 2612 | * @recursive: if 1 do a recursive copy. |
| 2613 | * |
| 2614 | * Do a copy of the node. |
| 2615 | * |
| 2616 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2617 | */ |
| 2618 | xmlNodePtr |
| 2619 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2620 | xmlNodePtr ret; |
| 2621 | |
| 2622 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2623 | return(ret); |
| 2624 | } |
| 2625 | |
| 2626 | /** |
| 2627 | * xmlCopyNodeList: |
| 2628 | * @node: the first node in the list. |
| 2629 | * |
| 2630 | * Do a recursive copy of the node list. |
| 2631 | * |
| 2632 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2633 | */ |
| 2634 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2635 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2636 | return(ret); |
| 2637 | } |
| 2638 | |
| 2639 | /** |
| 2640 | * xmlCopyElement: |
| 2641 | * @elem: the element |
| 2642 | * |
| 2643 | * Do a copy of the element definition. |
| 2644 | * |
| 2645 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2646 | xmlElementPtr |
| 2647 | xmlCopyElement(xmlElementPtr elem) { |
| 2648 | xmlElementPtr ret; |
| 2649 | |
| 2650 | if (elem == NULL) return(NULL); |
| 2651 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2652 | if (ret == NULL) return(NULL); |
| 2653 | if (!recursive) return(ret); |
| 2654 | if (elem->properties != NULL) |
| 2655 | ret->properties = xmlCopyPropList(elem->properties); |
| 2656 | |
| 2657 | if (elem->nsDef != NULL) |
| 2658 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2659 | if (elem->children != NULL) |
| 2660 | ret->children = xmlCopyElementList(elem->children); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2661 | return(ret); |
| 2662 | } |
| 2663 | */ |
| 2664 | |
| 2665 | /** |
| 2666 | * xmlCopyDtd: |
| 2667 | * @dtd: the dtd |
| 2668 | * |
| 2669 | * Do a copy of the dtd. |
| 2670 | * |
| 2671 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2672 | */ |
| 2673 | xmlDtdPtr |
| 2674 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2675 | xmlDtdPtr ret; |
| 2676 | |
| 2677 | if (dtd == NULL) return(NULL); |
| 2678 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2679 | if (ret == NULL) return(NULL); |
| 2680 | if (dtd->entities != NULL) |
| 2681 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2682 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2683 | if (dtd->notations != NULL) |
| 2684 | ret->notations = (void *) xmlCopyNotationTable( |
| 2685 | (xmlNotationTablePtr) dtd->notations); |
| 2686 | if (dtd->elements != NULL) |
| 2687 | ret->elements = (void *) xmlCopyElementTable( |
| 2688 | (xmlElementTablePtr) dtd->elements); |
| 2689 | if (dtd->attributes != NULL) |
| 2690 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2691 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2692 | return(ret); |
| 2693 | } |
| 2694 | |
| 2695 | /** |
| 2696 | * xmlCopyDoc: |
| 2697 | * @doc: the document |
| 2698 | * @recursive: if 1 do a recursive copy. |
| 2699 | * |
| 2700 | * Do a copy of the document info. If recursive, the content tree will |
| 2701 | * be copied too as well as Dtd, namespaces and entities. |
| 2702 | * |
| 2703 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2704 | */ |
| 2705 | xmlDocPtr |
| 2706 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2707 | xmlDocPtr ret; |
| 2708 | |
| 2709 | if (doc == NULL) return(NULL); |
| 2710 | ret = xmlNewDoc(doc->version); |
| 2711 | if (ret == NULL) return(NULL); |
| 2712 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2713 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2714 | if (doc->encoding != NULL) |
| 2715 | ret->encoding = xmlStrdup(doc->encoding); |
| 2716 | ret->compression = doc->compression; |
| 2717 | ret->standalone = doc->standalone; |
| 2718 | if (!recursive) return(ret); |
| 2719 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2720 | if (doc->intSubset != NULL) |
| 2721 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2722 | if (doc->oldNs != NULL) |
| 2723 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2724 | if (doc->children != NULL) { |
| 2725 | xmlNodePtr tmp; |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2726 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 2727 | (xmlNodePtr)ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2728 | ret->last = NULL; |
| 2729 | tmp = ret->children; |
| 2730 | while (tmp != NULL) { |
| 2731 | if (tmp->next == NULL) |
| 2732 | ret->last = tmp; |
| 2733 | tmp = tmp->next; |
| 2734 | } |
| 2735 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2736 | return(ret); |
| 2737 | } |
| 2738 | |
| 2739 | /************************************************************************ |
| 2740 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2741 | * Content access functions * |
| 2742 | * * |
| 2743 | ************************************************************************/ |
| 2744 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2745 | /** |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2746 | * xmlDocGetRootElement: |
| 2747 | * @doc: the document |
| 2748 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2749 | * Get the root element of the document (doc->children is a list |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2750 | * containing possibly comments, PIs, etc ...). |
| 2751 | * |
| 2752 | * Returns the xmlNodePtr for the root or NULL |
| 2753 | */ |
| 2754 | xmlNodePtr |
| 2755 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2756 | xmlNodePtr ret; |
| 2757 | |
| 2758 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2759 | ret = doc->children; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2760 | while (ret != NULL) { |
| 2761 | if (ret->type == XML_ELEMENT_NODE) |
| 2762 | return(ret); |
| 2763 | ret = ret->next; |
| 2764 | } |
| 2765 | return(ret); |
| 2766 | } |
| 2767 | |
| 2768 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2769 | * xmlDocSetRootElement: |
| 2770 | * @doc: the document |
| 2771 | * @root: the new document root element |
| 2772 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2773 | * Set the root element of the document (doc->children is a list |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2774 | * containing possibly comments, PIs, etc ...). |
| 2775 | * |
| 2776 | * Returns the old root element if any was found |
| 2777 | */ |
| 2778 | xmlNodePtr |
| 2779 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2780 | xmlNodePtr old = NULL; |
| 2781 | |
| 2782 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2783 | old = doc->children; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2784 | while (old != NULL) { |
| 2785 | if (old->type == XML_ELEMENT_NODE) |
| 2786 | break; |
| 2787 | old = old->next; |
| 2788 | } |
| 2789 | if (old == NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2790 | if (doc->children == NULL) { |
| 2791 | doc->children = root; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2792 | doc->last = root; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2793 | } else { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2794 | xmlAddSibling(doc->children, root); |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2795 | } |
| 2796 | } else { |
| 2797 | xmlReplaceNode(old, root); |
| 2798 | } |
| 2799 | return(old); |
| 2800 | } |
| 2801 | |
| 2802 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2803 | * xmlNodeSetLang: |
| 2804 | * @cur: the node being changed |
| 2805 | * @lang: the langage description |
| 2806 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2807 | * Set the language of a node, i.e. the values of the xml:lang |
| 2808 | * attribute. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2809 | */ |
| 2810 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2811 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2812 | if (cur == NULL) return; |
| 2813 | switch(cur->type) { |
| 2814 | case XML_TEXT_NODE: |
| 2815 | case XML_CDATA_SECTION_NODE: |
| 2816 | case XML_COMMENT_NODE: |
| 2817 | case XML_DOCUMENT_NODE: |
| 2818 | case XML_DOCUMENT_TYPE_NODE: |
| 2819 | case XML_DOCUMENT_FRAG_NODE: |
| 2820 | case XML_NOTATION_NODE: |
| 2821 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2822 | case XML_DTD_NODE: |
| 2823 | case XML_ELEMENT_DECL: |
| 2824 | case XML_ATTRIBUTE_DECL: |
| 2825 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2826 | case XML_PI_NODE: |
| 2827 | case XML_ENTITY_REF_NODE: |
| 2828 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2829 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2830 | #ifdef LIBXML_SGML_ENABLED |
| 2831 | case XML_SGML_DOCUMENT_NODE: |
| 2832 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2833 | case XML_XINCLUDE_START: |
| 2834 | case XML_XINCLUDE_END: |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 2835 | return; |
| 2836 | case XML_ELEMENT_NODE: |
| 2837 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2838 | break; |
| 2839 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2840 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * xmlNodeGetLang: |
| 2845 | * @cur: the node being checked |
| 2846 | * |
| 2847 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2848 | * attribute or the one carried by the nearest ancestor. |
| 2849 | * |
| 2850 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2851 | * It's up to the caller to free the memory. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2852 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2853 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2854 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2855 | xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2856 | |
| 2857 | while (cur != NULL) { |
| 2858 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2859 | if (lang != NULL) |
| 2860 | return(lang); |
| 2861 | cur = cur->parent; |
| 2862 | } |
| 2863 | return(NULL); |
| 2864 | } |
| 2865 | |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2866 | |
| 2867 | /** |
| 2868 | * xmlNodeSetSpacePreserve: |
| 2869 | * @cur: the node being changed |
| 2870 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 2871 | * |
| 2872 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 2873 | * value of the xml:space attribute. |
| 2874 | */ |
| 2875 | void |
| 2876 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
| 2877 | if (cur == NULL) return; |
| 2878 | switch(cur->type) { |
| 2879 | case XML_TEXT_NODE: |
| 2880 | case XML_CDATA_SECTION_NODE: |
| 2881 | case XML_COMMENT_NODE: |
| 2882 | case XML_DOCUMENT_NODE: |
| 2883 | case XML_DOCUMENT_TYPE_NODE: |
| 2884 | case XML_DOCUMENT_FRAG_NODE: |
| 2885 | case XML_NOTATION_NODE: |
| 2886 | case XML_HTML_DOCUMENT_NODE: |
| 2887 | case XML_DTD_NODE: |
| 2888 | case XML_ELEMENT_DECL: |
| 2889 | case XML_ATTRIBUTE_DECL: |
| 2890 | case XML_ENTITY_DECL: |
| 2891 | case XML_PI_NODE: |
| 2892 | case XML_ENTITY_REF_NODE: |
| 2893 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2894 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2895 | case XML_XINCLUDE_START: |
| 2896 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2897 | #ifdef LIBXML_SGML_ENABLED |
| 2898 | case XML_SGML_DOCUMENT_NODE: |
| 2899 | #endif |
| 2900 | return; |
| 2901 | case XML_ELEMENT_NODE: |
| 2902 | case XML_ATTRIBUTE_NODE: |
| 2903 | break; |
| 2904 | } |
| 2905 | switch (val) { |
| 2906 | case 0: |
| 2907 | xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default"); |
| 2908 | break; |
| 2909 | case 1: |
| 2910 | xmlSetProp(cur, BAD_CAST "xml:space", |
| 2911 | BAD_CAST "preserve"); |
| 2912 | break; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2916 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2917 | * xmlNodeGetSpacePreserve: |
| 2918 | * @cur: the node being checked |
| 2919 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2920 | * Searches the space preserving behaviour of a node, i.e. the values |
| 2921 | * of the xml:space attribute or the one carried by the nearest |
| 2922 | * ancestor. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2923 | * |
| 2924 | * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve" |
| 2925 | */ |
| 2926 | int |
| 2927 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 2928 | xmlChar *space; |
| 2929 | |
| 2930 | while (cur != NULL) { |
| 2931 | space = xmlGetProp(cur, BAD_CAST "xml:space"); |
| 2932 | if (space != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2933 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2934 | xmlFree(space); |
| 2935 | return(1); |
| 2936 | } |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2937 | if (xmlStrEqual(space, BAD_CAST "default")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2938 | xmlFree(space); |
| 2939 | return(0); |
| 2940 | } |
| 2941 | xmlFree(space); |
| 2942 | } |
| 2943 | cur = cur->parent; |
| 2944 | } |
| 2945 | return(-1); |
| 2946 | } |
| 2947 | |
| 2948 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2949 | * xmlNodeSetName: |
| 2950 | * @cur: the node being changed |
| 2951 | * @name: the new tag name |
| 2952 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2953 | * Set (or reset) the name of a node. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2954 | */ |
| 2955 | void |
| 2956 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2957 | if (cur == NULL) return; |
| 2958 | if (name == NULL) return; |
| 2959 | switch(cur->type) { |
| 2960 | case XML_TEXT_NODE: |
| 2961 | case XML_CDATA_SECTION_NODE: |
| 2962 | case XML_COMMENT_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2963 | case XML_DOCUMENT_TYPE_NODE: |
| 2964 | case XML_DOCUMENT_FRAG_NODE: |
| 2965 | case XML_NOTATION_NODE: |
| 2966 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2967 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2968 | case XML_XINCLUDE_START: |
| 2969 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2970 | #ifdef LIBXML_SGML_ENABLED |
| 2971 | case XML_SGML_DOCUMENT_NODE: |
| 2972 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2973 | return; |
| 2974 | case XML_ELEMENT_NODE: |
| 2975 | case XML_ATTRIBUTE_NODE: |
| 2976 | case XML_PI_NODE: |
| 2977 | case XML_ENTITY_REF_NODE: |
| 2978 | case XML_ENTITY_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2979 | case XML_DTD_NODE: |
| 2980 | case XML_DOCUMENT_NODE: |
| 2981 | case XML_ELEMENT_DECL: |
| 2982 | case XML_ATTRIBUTE_DECL: |
| 2983 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2984 | break; |
| 2985 | } |
| 2986 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 2987 | cur->name = xmlStrdup(name); |
| 2988 | } |
| 2989 | |
| 2990 | /** |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2991 | * xmlNodeSetBase: |
| 2992 | * @cur: the node being changed |
| 2993 | * @uri: the new base URI |
| 2994 | * |
| 2995 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 2996 | * xml:base attribute. |
| 2997 | */ |
| 2998 | void |
| 2999 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
| 3000 | if (cur == NULL) return; |
| 3001 | switch(cur->type) { |
| 3002 | case XML_TEXT_NODE: |
| 3003 | case XML_CDATA_SECTION_NODE: |
| 3004 | case XML_COMMENT_NODE: |
| 3005 | case XML_DOCUMENT_NODE: |
| 3006 | case XML_DOCUMENT_TYPE_NODE: |
| 3007 | case XML_DOCUMENT_FRAG_NODE: |
| 3008 | case XML_NOTATION_NODE: |
| 3009 | case XML_HTML_DOCUMENT_NODE: |
| 3010 | case XML_DTD_NODE: |
| 3011 | case XML_ELEMENT_DECL: |
| 3012 | case XML_ATTRIBUTE_DECL: |
| 3013 | case XML_ENTITY_DECL: |
| 3014 | case XML_PI_NODE: |
| 3015 | case XML_ENTITY_REF_NODE: |
| 3016 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3017 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3018 | case XML_XINCLUDE_START: |
| 3019 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 3020 | #ifdef LIBXML_SGML_ENABLED |
| 3021 | case XML_SGML_DOCUMENT_NODE: |
| 3022 | #endif |
| 3023 | return; |
| 3024 | case XML_ELEMENT_NODE: |
| 3025 | case XML_ATTRIBUTE_NODE: |
| 3026 | break; |
| 3027 | } |
| 3028 | xmlSetProp(cur, BAD_CAST "xml:base", uri); |
| 3029 | } |
| 3030 | |
| 3031 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3032 | * xmlDocumentGetBase: |
| 3033 | * @doc: the document |
| 3034 | * |
| 3035 | * Searches for the Document BASE URL. The code should work on both XML |
| 3036 | * and HTML document. |
| 3037 | * It returns the base as defined in RFC 2396 section |
| 3038 | * 5.1.3. Base URI from the Retrieval URI |
| 3039 | * However it does not return the computed base (5.1.1 and 5.1.2), use |
| 3040 | * xmlNodeGetBase() for this |
| 3041 | * |
| 3042 | * Returns a pointer to the base URL, or NULL if not found |
| 3043 | * It's up to the caller to free the memory. |
| 3044 | */ |
| 3045 | xmlChar * |
| 3046 | xmlDocumentGetBase(xmlDocPtr doc) { |
| 3047 | if (doc == NULL) |
| 3048 | return(NULL); |
| 3049 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 3050 | if (doc->URL != NULL) |
| 3051 | return(xmlStrdup(doc->URL)); |
| 3052 | return(NULL); |
| 3053 | } |
| 3054 | if (doc->URL != NULL) |
| 3055 | return(xmlStrdup(doc->URL)); |
| 3056 | return(NULL); |
| 3057 | } |
| 3058 | |
| 3059 | /** |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3060 | * xmlNodeGetBase: |
| 3061 | * @doc: the document the node pertains to |
| 3062 | * @cur: the node being checked |
| 3063 | * |
| 3064 | * Searches for the BASE URL. The code should work on both XML |
| 3065 | * and HTML document even if base mechanisms are completely different. |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3066 | * It returns the base as defined in RFC 2396 sections |
| 3067 | * 5.1.1. Base URI within Document Content |
| 3068 | * and |
| 3069 | * 5.1.2. Base URI from the Encapsulating Entity |
| 3070 | * However it does not return the document base (5.1.3), use |
| 3071 | * xmlDocumentGetBase() for this |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3072 | * |
| 3073 | * Returns a pointer to the base URL, or NULL if not found |
| 3074 | * It's up to the caller to free the memory. |
| 3075 | */ |
| 3076 | xmlChar * |
| 3077 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 3078 | xmlChar *base; |
| 3079 | |
| 3080 | if ((cur == NULL) && (doc == NULL)) |
| 3081 | return(NULL); |
| 3082 | if (doc == NULL) doc = cur->doc; |
| 3083 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3084 | cur = doc->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3085 | while ((cur != NULL) && (cur->name != NULL)) { |
| 3086 | if (cur->type != XML_ELEMENT_NODE) { |
| 3087 | cur = cur->next; |
| 3088 | continue; |
| 3089 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3090 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3091 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3092 | continue; |
| 3093 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3094 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3095 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3096 | continue; |
| 3097 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3098 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 3099 | return(xmlGetProp(cur, BAD_CAST "href")); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3100 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3101 | cur = cur->next; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3102 | } |
| 3103 | return(NULL); |
| 3104 | } |
| 3105 | while (cur != NULL) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3106 | if (cur->type == XML_ENTITY_DECL) { |
| 3107 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3108 | return(xmlStrdup(ent->URI)); |
| 3109 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3110 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 3111 | if (base != NULL) |
| 3112 | return(base); |
| 3113 | cur = cur->parent; |
| 3114 | } |
| 3115 | return(NULL); |
| 3116 | } |
| 3117 | |
| 3118 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3119 | * xmlNodeGetContent: |
| 3120 | * @cur: the node being read |
| 3121 | * |
| 3122 | * Read the value of a node, this can be either the text carried |
| 3123 | * directly by this node if it's a TEXT node or the aggregate string |
| 3124 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 3125 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3126 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3127 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3128 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3129 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3130 | xmlNodeGetContent(xmlNodePtr cur) { |
| 3131 | if (cur == NULL) return(NULL); |
| 3132 | switch (cur->type) { |
| 3133 | case XML_DOCUMENT_FRAG_NODE: |
| 3134 | case XML_ELEMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3135 | return(xmlNodeListGetString(cur->doc, cur->children, 1)); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3136 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3137 | case XML_ATTRIBUTE_NODE: { |
| 3138 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3139 | if (attr->parent != NULL) |
| 3140 | return(xmlNodeListGetString(attr->parent->doc, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3141 | else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3142 | return(xmlNodeListGetString(NULL, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3145 | case XML_COMMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3146 | case XML_PI_NODE: |
| 3147 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3148 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3149 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3150 | #else |
| 3151 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3152 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3153 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3154 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3155 | /* |
| 3156 | * Locate the entity, and get it's content |
| 3157 | * @@@ |
| 3158 | */ |
| 3159 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3160 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3161 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3162 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3163 | case XML_DOCUMENT_TYPE_NODE: |
| 3164 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3165 | case XML_DTD_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3166 | case XML_XINCLUDE_START: |
| 3167 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3168 | #ifdef LIBXML_SGML_ENABLED |
| 3169 | case XML_SGML_DOCUMENT_NODE: |
| 3170 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3171 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3172 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3173 | return(xmlStrdup(((xmlNsPtr)cur)->href)); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3174 | case XML_ELEMENT_DECL: |
| 3175 | /* TODO !!! */ |
| 3176 | return(NULL); |
| 3177 | case XML_ATTRIBUTE_DECL: |
| 3178 | /* TODO !!! */ |
| 3179 | return(NULL); |
| 3180 | case XML_ENTITY_DECL: |
| 3181 | /* TODO !!! */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3182 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3183 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3184 | case XML_TEXT_NODE: |
| 3185 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3186 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3187 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3188 | #else |
| 3189 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3190 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3191 | return(NULL); |
| 3192 | } |
| 3193 | return(NULL); |
| 3194 | } |
| 3195 | |
| 3196 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3197 | * xmlNodeSetContent: |
| 3198 | * @cur: the node being modified |
| 3199 | * @content: the new value of the content |
| 3200 | * |
| 3201 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3202 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3203 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3204 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3205 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3206 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3207 | xmlGenericError(xmlGenericErrorContext, |
| 3208 | "xmlNodeSetContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3209 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3210 | return; |
| 3211 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3212 | switch (cur->type) { |
| 3213 | case XML_DOCUMENT_FRAG_NODE: |
| 3214 | case XML_ELEMENT_NODE: |
| 3215 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3216 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3217 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3218 | #else |
| 3219 | xmlBufferFree(cur->content); |
| 3220 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3221 | cur->content = NULL; |
| 3222 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3223 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3224 | cur->children = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3225 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3226 | break; |
| 3227 | case XML_ATTRIBUTE_NODE: |
| 3228 | break; |
| 3229 | case XML_TEXT_NODE: |
| 3230 | case XML_CDATA_SECTION_NODE: |
| 3231 | case XML_ENTITY_REF_NODE: |
| 3232 | case XML_ENTITY_NODE: |
| 3233 | case XML_PI_NODE: |
| 3234 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3235 | if (cur->content != NULL) { |
| 3236 | #ifndef XML_USE_BUFFER_CONTENT |
| 3237 | xmlFree(cur->content); |
| 3238 | #else |
| 3239 | xmlBufferFree(cur->content); |
| 3240 | #endif |
| 3241 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3242 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3243 | cur->last = cur->children = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3244 | if (content != NULL) { |
| 3245 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3246 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3247 | #else |
| 3248 | cur->content = xmlBufferCreateSize(0); |
| 3249 | xmlBufferSetAllocationScheme(cur->content, |
| 3250 | xmlGetBufferAllocationScheme()); |
| 3251 | xmlBufferAdd(cur->content, content, -1); |
| 3252 | #endif |
| 3253 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3254 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3255 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3256 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3257 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3258 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3259 | case XML_XINCLUDE_START: |
| 3260 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3261 | #ifdef LIBXML_SGML_ENABLED |
| 3262 | case XML_SGML_DOCUMENT_NODE: |
| 3263 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3264 | break; |
| 3265 | case XML_NOTATION_NODE: |
| 3266 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3267 | case XML_DTD_NODE: |
| 3268 | break; |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3269 | case XML_NAMESPACE_DECL: |
| 3270 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3271 | case XML_ELEMENT_DECL: |
| 3272 | /* TODO !!! */ |
| 3273 | break; |
| 3274 | case XML_ATTRIBUTE_DECL: |
| 3275 | /* TODO !!! */ |
| 3276 | break; |
| 3277 | case XML_ENTITY_DECL: |
| 3278 | /* TODO !!! */ |
| 3279 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3280 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3283 | /** |
| 3284 | * xmlNodeSetContentLen: |
| 3285 | * @cur: the node being modified |
| 3286 | * @content: the new value of the content |
| 3287 | * @len: the size of @content |
| 3288 | * |
| 3289 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3290 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3291 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3292 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3293 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3294 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3295 | xmlGenericError(xmlGenericErrorContext, |
| 3296 | "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3297 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3298 | return; |
| 3299 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3300 | switch (cur->type) { |
| 3301 | case XML_DOCUMENT_FRAG_NODE: |
| 3302 | case XML_ELEMENT_NODE: |
| 3303 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3304 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3305 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3306 | #else |
| 3307 | xmlBufferFree(cur->content); |
| 3308 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3309 | cur->content = NULL; |
| 3310 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3311 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3312 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3313 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3314 | break; |
| 3315 | case XML_ATTRIBUTE_NODE: |
| 3316 | break; |
| 3317 | case XML_TEXT_NODE: |
| 3318 | case XML_CDATA_SECTION_NODE: |
| 3319 | case XML_ENTITY_REF_NODE: |
| 3320 | case XML_ENTITY_NODE: |
| 3321 | case XML_PI_NODE: |
| 3322 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3323 | case XML_NOTATION_NODE: |
| 3324 | if (cur->content != NULL) { |
| 3325 | #ifndef XML_USE_BUFFER_CONTENT |
| 3326 | xmlFree(cur->content); |
| 3327 | #else |
| 3328 | xmlBufferFree(cur->content); |
| 3329 | #endif |
| 3330 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3331 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3332 | cur->children = cur->last = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3333 | if (content != NULL) { |
| 3334 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3335 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3336 | #else |
| 3337 | cur->content = xmlBufferCreateSize(len); |
| 3338 | xmlBufferSetAllocationScheme(cur->content, |
| 3339 | xmlGetBufferAllocationScheme()); |
| 3340 | xmlBufferAdd(cur->content, content, len); |
| 3341 | #endif |
| 3342 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3343 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3344 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3345 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3346 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3347 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3348 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3349 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3350 | case XML_XINCLUDE_START: |
| 3351 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3352 | #ifdef LIBXML_SGML_ENABLED |
| 3353 | case XML_SGML_DOCUMENT_NODE: |
| 3354 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3355 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3356 | case XML_ELEMENT_DECL: |
| 3357 | /* TODO !!! */ |
| 3358 | break; |
| 3359 | case XML_ATTRIBUTE_DECL: |
| 3360 | /* TODO !!! */ |
| 3361 | break; |
| 3362 | case XML_ENTITY_DECL: |
| 3363 | /* TODO !!! */ |
| 3364 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3365 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3368 | /** |
| 3369 | * xmlNodeAddContentLen: |
| 3370 | * @cur: the node being modified |
| 3371 | * @content: extra content |
| 3372 | * @len: the size of @content |
| 3373 | * |
| 3374 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3375 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3376 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3377 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3378 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3379 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3380 | xmlGenericError(xmlGenericErrorContext, |
| 3381 | "xmlNodeAddContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3382 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3383 | return; |
| 3384 | } |
| 3385 | if (len <= 0) return; |
| 3386 | switch (cur->type) { |
| 3387 | case XML_DOCUMENT_FRAG_NODE: |
| 3388 | case XML_ELEMENT_NODE: { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3389 | xmlNodePtr last = NULL, newNode; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3390 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3391 | if (cur->children != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3392 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3393 | } else { |
| 3394 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3395 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3396 | cur->children = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3397 | #else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3398 | cur->children = xmlStringGetNodeList(cur->doc, |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3399 | xmlBufferContent(cur->content)); |
| 3400 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3401 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3402 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3403 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3404 | #else |
| 3405 | xmlBufferFree(cur->content); |
| 3406 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3407 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3408 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3409 | } |
| 3410 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3411 | newNode = xmlNewTextLen(content, len); |
| 3412 | if (newNode != NULL) { |
| 3413 | xmlAddChild(cur, newNode); |
| 3414 | if ((last != NULL) && (last->next == newNode)) { |
| 3415 | xmlTextMerge(last, newNode); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3416 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3417 | } |
| 3418 | break; |
| 3419 | } |
| 3420 | case XML_ATTRIBUTE_NODE: |
| 3421 | break; |
| 3422 | case XML_TEXT_NODE: |
| 3423 | case XML_CDATA_SECTION_NODE: |
| 3424 | case XML_ENTITY_REF_NODE: |
| 3425 | case XML_ENTITY_NODE: |
| 3426 | case XML_PI_NODE: |
| 3427 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3428 | case XML_NOTATION_NODE: |
| 3429 | if (content != NULL) { |
| 3430 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3431 | cur->content = xmlStrncat(cur->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3432 | #else |
| 3433 | xmlBufferAdd(cur->content, content, len); |
| 3434 | #endif |
| 3435 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3436 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3437 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3438 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3439 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3440 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3441 | case XML_XINCLUDE_START: |
| 3442 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3443 | #ifdef LIBXML_SGML_ENABLED |
| 3444 | case XML_SGML_DOCUMENT_NODE: |
| 3445 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3446 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3447 | case XML_ELEMENT_DECL: |
| 3448 | case XML_ATTRIBUTE_DECL: |
| 3449 | case XML_ENTITY_DECL: |
| 3450 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3451 | } |
| 3452 | } |
| 3453 | |
| 3454 | /** |
| 3455 | * xmlNodeAddContent: |
| 3456 | * @cur: the node being modified |
| 3457 | * @content: extra content |
| 3458 | * |
| 3459 | * Append the extra substring to the node content. |
| 3460 | */ |
| 3461 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3462 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3463 | int len; |
| 3464 | |
| 3465 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3466 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3467 | xmlGenericError(xmlGenericErrorContext, |
| 3468 | "xmlNodeAddContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3469 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3470 | return; |
| 3471 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3472 | if (content == NULL) return; |
| 3473 | len = xmlStrlen(content); |
| 3474 | xmlNodeAddContentLen(cur, content, len); |
| 3475 | } |
| 3476 | |
| 3477 | /** |
| 3478 | * xmlTextMerge: |
| 3479 | * @first: the first text node |
| 3480 | * @second: the second text node being merged |
| 3481 | * |
| 3482 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3483 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3484 | */ |
| 3485 | xmlNodePtr |
| 3486 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 3487 | if (first == NULL) return(second); |
| 3488 | if (second == NULL) return(first); |
| 3489 | if (first->type != XML_TEXT_NODE) return(first); |
| 3490 | if (second->type != XML_TEXT_NODE) return(first); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3491 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3492 | xmlNodeAddContent(first, second->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3493 | #else |
| 3494 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 3495 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3496 | xmlUnlinkNode(second); |
| 3497 | xmlFreeNode(second); |
| 3498 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3501 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3502 | * xmlGetNsList: |
| 3503 | * @doc: the document |
| 3504 | * @node: the current node |
| 3505 | * |
| 3506 | * Search all the namespace applying to a given element. |
| 3507 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 3508 | * that need to be freed by the caller or NULL if no |
| 3509 | * namespace if defined |
| 3510 | */ |
| 3511 | xmlNsPtr * |
| 3512 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 3513 | xmlNsPtr cur; |
| 3514 | xmlNsPtr *ret = NULL; |
| 3515 | int nbns = 0; |
| 3516 | int maxns = 10; |
| 3517 | int i; |
| 3518 | |
| 3519 | while (node != NULL) { |
| 3520 | cur = node->nsDef; |
| 3521 | while (cur != NULL) { |
| 3522 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3523 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3524 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3525 | xmlGenericError(xmlGenericErrorContext, |
| 3526 | "xmlGetNsList : out of memory!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3527 | return(NULL); |
| 3528 | } |
| 3529 | ret[nbns] = NULL; |
| 3530 | } |
| 3531 | for (i = 0;i < nbns;i++) { |
| 3532 | if ((cur->prefix == ret[i]->prefix) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3533 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3534 | } |
| 3535 | if (i >= nbns) { |
| 3536 | if (nbns >= maxns) { |
| 3537 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3538 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3539 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 3540 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3541 | xmlGenericError(xmlGenericErrorContext, |
| 3542 | "xmlGetNsList : realloc failed!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3543 | return(NULL); |
| 3544 | } |
| 3545 | } |
| 3546 | ret[nbns++] = cur; |
| 3547 | ret[nbns] = NULL; |
| 3548 | } |
| 3549 | |
| 3550 | cur = cur->next; |
| 3551 | } |
| 3552 | node = node->parent; |
| 3553 | } |
| 3554 | return(ret); |
| 3555 | } |
| 3556 | |
| 3557 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3558 | * xmlSearchNs: |
| 3559 | * @doc: the document |
| 3560 | * @node: the current node |
| 3561 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3562 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3563 | * Search a Ns registered under a given name space for a document. |
| 3564 | * recurse on the parents until it finds the defined namespace |
| 3565 | * or return NULL otherwise. |
| 3566 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3567 | * We don't allow to cross entities boundaries. If you don't declare |
| 3568 | * the namespace within those you will be in troubles !!! A warning |
| 3569 | * is generated to cover this case. |
| 3570 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3571 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3572 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3573 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3574 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3575 | xmlNsPtr cur; |
| 3576 | |
Daniel Veillard | 62ba71e | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 3577 | if (node == NULL) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3578 | while (node != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3579 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 3580 | (node->type == XML_ENTITY_NODE) || |
| 3581 | (node->type == XML_ENTITY_DECL)) |
| 3582 | return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3583 | if (node->type == XML_ELEMENT_NODE) { |
| 3584 | cur = node->nsDef; |
| 3585 | while (cur != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3586 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 3587 | (cur->href != NULL)) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3588 | return(cur); |
| 3589 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3590 | (cur->href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3591 | (xmlStrEqual(cur->prefix, nameSpace))) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3592 | return(cur); |
| 3593 | cur = cur->next; |
| 3594 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3595 | } |
| 3596 | node = node->parent; |
| 3597 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3598 | return(NULL); |
| 3599 | } |
| 3600 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3601 | /** |
| 3602 | * xmlSearchNsByHref: |
| 3603 | * @doc: the document |
| 3604 | * @node: the current node |
| 3605 | * @href: the namespace value |
| 3606 | * |
| 3607 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 3608 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3609 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3610 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3611 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3612 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3613 | xmlNsPtr cur; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3614 | xmlNodePtr orig = node; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3615 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3616 | if ((node == NULL) || (href == NULL)) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3617 | while (node != NULL) { |
| 3618 | cur = node->nsDef; |
| 3619 | while (cur != NULL) { |
| 3620 | if ((cur->href != NULL) && (href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3621 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3622 | /* |
| 3623 | * Check that the prefix is not shadowed between orig and node |
| 3624 | */ |
| 3625 | xmlNodePtr check = orig; |
| 3626 | xmlNsPtr tst; |
| 3627 | |
| 3628 | while (check != node) { |
| 3629 | tst = check->nsDef; |
| 3630 | while (tst != NULL) { |
| 3631 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 3632 | goto shadowed; |
| 3633 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3634 | (xmlStrEqual(tst->prefix, cur->prefix))) |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3635 | goto shadowed; |
| 3636 | tst = tst->next; |
| 3637 | } |
Daniel Veillard | c4f4f0b | 2000-10-29 17:46:30 +0000 | [diff] [blame] | 3638 | check = check->parent; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3639 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3640 | return(cur); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3641 | } |
| 3642 | shadowed: |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3643 | cur = cur->next; |
| 3644 | } |
| 3645 | node = node->parent; |
| 3646 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3647 | return(NULL); |
| 3648 | } |
| 3649 | |
| 3650 | /** |
| 3651 | * xmlNewReconciliedNs |
| 3652 | * @doc: the document |
| 3653 | * @tree: a node expected to hold the new namespace |
| 3654 | * @ns: the original namespace |
| 3655 | * |
| 3656 | * This function tries to locate a namespace definition in a tree |
| 3657 | * ancestors, or create a new namespace definition node similar to |
| 3658 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 3659 | * null (default namespace) or reused within the subtree defined by |
| 3660 | * @tree or on one of its ancestors then a new prefix is generated. |
| 3661 | * Returns the (new) namespace definition or NULL in case of error |
| 3662 | */ |
| 3663 | xmlNsPtr |
| 3664 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 3665 | xmlNsPtr def; |
| 3666 | xmlChar prefix[50]; |
| 3667 | int counter = 1; |
| 3668 | |
| 3669 | if (tree == NULL) { |
| 3670 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3671 | xmlGenericError(xmlGenericErrorContext, |
| 3672 | "xmlNewReconciliedNs : tree == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3673 | #endif |
| 3674 | return(NULL); |
| 3675 | } |
| 3676 | if (ns == NULL) { |
| 3677 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3678 | xmlGenericError(xmlGenericErrorContext, |
| 3679 | "xmlNewReconciliedNs : ns == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3680 | #endif |
| 3681 | return(NULL); |
| 3682 | } |
| 3683 | /* |
| 3684 | * Search an existing namespace definition inherited. |
| 3685 | */ |
| 3686 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 3687 | if (def != NULL) |
| 3688 | return(def); |
| 3689 | |
| 3690 | /* |
| 3691 | * Find a close prefix which is not already in use. |
| 3692 | * Let's strip namespace prefixes longer than 20 chars ! |
| 3693 | */ |
| 3694 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 3695 | def = xmlSearchNs(doc, tree, prefix); |
| 3696 | while (def != NULL) { |
| 3697 | if (counter > 1000) return(NULL); |
| 3698 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 3699 | def = xmlSearchNs(doc, tree, prefix); |
| 3700 | } |
| 3701 | |
| 3702 | /* |
| 3703 | * Ok, now we are ready to create a new one. |
| 3704 | */ |
| 3705 | def = xmlNewNs(tree, ns->href, prefix); |
| 3706 | return(def); |
| 3707 | } |
| 3708 | |
| 3709 | /** |
| 3710 | * xmlReconciliateNs |
| 3711 | * @doc: the document |
| 3712 | * @tree: a node defining the subtree to reconciliate |
| 3713 | * |
| 3714 | * This function checks that all the namespaces declared within the given |
| 3715 | * tree are properly declared. This is needed for example after Copy or Cut |
| 3716 | * and then paste operations. The subtree may still hold pointers to |
| 3717 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3718 | * as possible the function try tu reuse the existing namespaces found in |
| 3719 | * the new environment. If not possible the new namespaces are redeclared |
| 3720 | * on @tree at the top of the given subtree. |
| 3721 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3722 | */ |
| 3723 | int |
| 3724 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3725 | xmlNsPtr *oldNs = NULL; |
| 3726 | xmlNsPtr *newNs = NULL; |
| 3727 | int sizeCache = 0; |
| 3728 | int nbCache = 0; |
| 3729 | |
| 3730 | xmlNsPtr n; |
| 3731 | xmlNodePtr node = tree; |
| 3732 | xmlAttrPtr attr; |
| 3733 | int ret = 0, i; |
| 3734 | |
| 3735 | while (node != NULL) { |
| 3736 | /* |
| 3737 | * Reconciliate the node namespace |
| 3738 | */ |
| 3739 | if (node->ns != NULL) { |
| 3740 | /* |
| 3741 | * initialize the cache if needed |
| 3742 | */ |
| 3743 | if (sizeCache == 0) { |
| 3744 | sizeCache = 10; |
| 3745 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3746 | sizeof(xmlNsPtr)); |
| 3747 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3748 | xmlGenericError(xmlGenericErrorContext, |
| 3749 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3750 | return(-1); |
| 3751 | } |
| 3752 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3753 | sizeof(xmlNsPtr)); |
| 3754 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3755 | xmlGenericError(xmlGenericErrorContext, |
| 3756 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3757 | xmlFree(oldNs); |
| 3758 | return(-1); |
| 3759 | } |
| 3760 | } |
| 3761 | for (i = 0;i < nbCache;i++) { |
| 3762 | if (oldNs[i] == node->ns) { |
| 3763 | node->ns = newNs[i]; |
| 3764 | break; |
| 3765 | } |
| 3766 | } |
| 3767 | if (i == nbCache) { |
| 3768 | /* |
| 3769 | * Ok we need to recreate a new namespace definition |
| 3770 | */ |
| 3771 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3772 | if (n != NULL) { /* :-( what if else ??? */ |
| 3773 | /* |
| 3774 | * check if we need to grow the cache buffers. |
| 3775 | */ |
| 3776 | if (sizeCache <= nbCache) { |
| 3777 | sizeCache *= 2; |
| 3778 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3779 | sizeof(xmlNsPtr)); |
| 3780 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3781 | xmlGenericError(xmlGenericErrorContext, |
| 3782 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3783 | xmlFree(newNs); |
| 3784 | return(-1); |
| 3785 | } |
| 3786 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3787 | sizeof(xmlNsPtr)); |
| 3788 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3789 | xmlGenericError(xmlGenericErrorContext, |
| 3790 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3791 | xmlFree(oldNs); |
| 3792 | return(-1); |
| 3793 | } |
| 3794 | } |
| 3795 | newNs[nbCache] = n; |
| 3796 | oldNs[nbCache++] = node->ns; |
| 3797 | node->ns = n; |
| 3798 | } |
| 3799 | } |
| 3800 | } |
| 3801 | /* |
| 3802 | * now check for namespace hold by attributes on the node. |
| 3803 | */ |
| 3804 | attr = node->properties; |
| 3805 | while (attr != NULL) { |
| 3806 | if (attr->ns != NULL) { |
| 3807 | /* |
| 3808 | * initialize the cache if needed |
| 3809 | */ |
| 3810 | if (sizeCache == 0) { |
| 3811 | sizeCache = 10; |
| 3812 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3813 | sizeof(xmlNsPtr)); |
| 3814 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3815 | xmlGenericError(xmlGenericErrorContext, |
| 3816 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3817 | return(-1); |
| 3818 | } |
| 3819 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3820 | sizeof(xmlNsPtr)); |
| 3821 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3822 | xmlGenericError(xmlGenericErrorContext, |
| 3823 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3824 | xmlFree(oldNs); |
| 3825 | return(-1); |
| 3826 | } |
| 3827 | } |
| 3828 | for (i = 0;i < nbCache;i++) { |
| 3829 | if (oldNs[i] == attr->ns) { |
| 3830 | node->ns = newNs[i]; |
| 3831 | break; |
| 3832 | } |
| 3833 | } |
| 3834 | if (i == nbCache) { |
| 3835 | /* |
| 3836 | * Ok we need to recreate a new namespace definition |
| 3837 | */ |
| 3838 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3839 | if (n != NULL) { /* :-( what if else ??? */ |
| 3840 | /* |
| 3841 | * check if we need to grow the cache buffers. |
| 3842 | */ |
| 3843 | if (sizeCache <= nbCache) { |
| 3844 | sizeCache *= 2; |
| 3845 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3846 | sizeof(xmlNsPtr)); |
| 3847 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3848 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3849 | "xmlReconciliateNs : memory pbm\n"); |
| 3850 | xmlFree(newNs); |
| 3851 | return(-1); |
| 3852 | } |
| 3853 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3854 | sizeof(xmlNsPtr)); |
| 3855 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3856 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3857 | "xmlReconciliateNs : memory pbm\n"); |
| 3858 | xmlFree(oldNs); |
| 3859 | return(-1); |
| 3860 | } |
| 3861 | } |
| 3862 | newNs[nbCache] = n; |
| 3863 | oldNs[nbCache++] = attr->ns; |
| 3864 | attr->ns = n; |
| 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | attr = attr->next; |
| 3869 | } |
| 3870 | |
| 3871 | /* |
| 3872 | * Browse the full subtree, deep first |
| 3873 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3874 | if (node->children != NULL) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3875 | /* deep first */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3876 | node = node->children; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3877 | } else if ((node != tree) && (node->next != NULL)) { |
| 3878 | /* then siblings */ |
| 3879 | node = node->next; |
| 3880 | } else if (node != tree) { |
| 3881 | /* go up to parents->next if needed */ |
| 3882 | while (node != tree) { |
| 3883 | if (node->parent != NULL) |
| 3884 | node = node->parent; |
| 3885 | if ((node != tree) && (node->next != NULL)) { |
| 3886 | node = node->next; |
| 3887 | break; |
| 3888 | } |
| 3889 | if (node->parent == NULL) { |
| 3890 | node = NULL; |
| 3891 | break; |
| 3892 | } |
| 3893 | } |
| 3894 | /* exit condition */ |
| 3895 | if (node == tree) |
| 3896 | node = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3897 | } |
| 3898 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3899 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3902 | /** |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3903 | * xmlHasProp: |
| 3904 | * @node: the node |
| 3905 | * @name: the attribute name |
| 3906 | * |
| 3907 | * Search an attribute associated to a node |
| 3908 | * This function also looks in DTD attribute declaration for #FIXED or |
| 3909 | * default declaration values unless DTD use has been turned off. |
| 3910 | * |
| 3911 | * Returns the attribute or the attribute declaration or NULL if |
| 3912 | * neither was found. |
| 3913 | */ |
| 3914 | xmlAttrPtr |
| 3915 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 3916 | xmlAttrPtr prop; |
| 3917 | xmlDocPtr doc; |
| 3918 | |
| 3919 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3920 | /* |
| 3921 | * Check on the properties attached to the node |
| 3922 | */ |
| 3923 | prop = node->properties; |
| 3924 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3925 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3926 | return(prop); |
| 3927 | } |
| 3928 | prop = prop->next; |
| 3929 | } |
| 3930 | if (!xmlCheckDTD) return(NULL); |
| 3931 | |
| 3932 | /* |
| 3933 | * Check if there is a default declaration in the internal |
| 3934 | * or external subsets |
| 3935 | */ |
| 3936 | doc = node->doc; |
| 3937 | if (doc != NULL) { |
| 3938 | xmlAttributePtr attrDecl; |
| 3939 | if (doc->intSubset != NULL) { |
| 3940 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3941 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3942 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3943 | if (attrDecl != NULL) |
| 3944 | return((xmlAttrPtr) attrDecl); |
| 3945 | } |
| 3946 | } |
| 3947 | return(NULL); |
| 3948 | } |
| 3949 | |
| 3950 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3951 | * xmlGetProp: |
| 3952 | * @node: the node |
| 3953 | * @name: the attribute name |
| 3954 | * |
| 3955 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3956 | * This does the entity substitution. |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3957 | * This function looks in DTD attribute declaration for #FIXED or |
| 3958 | * default declaration values unless DTD use has been turned off. |
| 3959 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3960 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3961 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3962 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3963 | xmlChar * |
| 3964 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3965 | xmlAttrPtr prop; |
| 3966 | xmlDocPtr doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3967 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3968 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3969 | /* |
| 3970 | * Check on the properties attached to the node |
| 3971 | */ |
| 3972 | prop = node->properties; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3973 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3974 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3975 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3976 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3977 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3978 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3979 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3980 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3981 | prop = prop->next; |
| 3982 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3983 | if (!xmlCheckDTD) return(NULL); |
| 3984 | |
| 3985 | /* |
| 3986 | * Check if there is a default declaration in the internal |
| 3987 | * or external subsets |
| 3988 | */ |
| 3989 | doc = node->doc; |
| 3990 | if (doc != NULL) { |
| 3991 | xmlAttributePtr attrDecl; |
| 3992 | if (doc->intSubset != NULL) { |
| 3993 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3994 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3995 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | f967b90 | 2000-01-17 16:06:10 +0000 | [diff] [blame] | 3996 | if (attrDecl != NULL) |
| 3997 | return(xmlStrdup(attrDecl->defaultValue)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | return(NULL); |
| 4001 | } |
| 4002 | |
| 4003 | /** |
| 4004 | * xmlGetNsProp: |
| 4005 | * @node: the node |
| 4006 | * @name: the attribute name |
| 4007 | * @namespace: the URI of the namespace |
| 4008 | * |
| 4009 | * Search and get the value of an attribute associated to a node |
| 4010 | * This attribute has to be anchored in the namespace specified. |
| 4011 | * This does the entity substitution. |
| 4012 | * This function looks in DTD attribute declaration for #FIXED or |
| 4013 | * default declaration values unless DTD use has been turned off. |
| 4014 | * |
| 4015 | * Returns the attribute value or NULL if not found. |
| 4016 | * It's up to the caller to free the memory. |
| 4017 | */ |
| 4018 | xmlChar * |
| 4019 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
| 4020 | xmlAttrPtr prop = node->properties; |
| 4021 | xmlDocPtr doc; |
| 4022 | xmlNsPtr ns; |
| 4023 | |
| 4024 | if (namespace == NULL) |
| 4025 | return(xmlGetProp(node, name)); |
| 4026 | while (prop != NULL) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4027 | /* |
| 4028 | * One need to have |
| 4029 | * - same attribute names |
| 4030 | * - and the attribute carrying that namespace |
| 4031 | * or |
| 4032 | * no namespace on the attribute and the element carrying it |
| 4033 | */ |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4034 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4035 | (((prop->ns == NULL) && (node->ns != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4036 | (xmlStrEqual(node->ns->href, namespace))) || |
| 4037 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, namespace))))) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4038 | xmlChar *ret; |
| 4039 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4040 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4041 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 4042 | return(ret); |
| 4043 | } |
| 4044 | prop = prop->next; |
| 4045 | } |
| 4046 | if (!xmlCheckDTD) return(NULL); |
| 4047 | |
| 4048 | /* |
| 4049 | * Check if there is a default declaration in the internal |
| 4050 | * or external subsets |
| 4051 | */ |
| 4052 | doc = node->doc; |
| 4053 | if (doc != NULL) { |
| 4054 | xmlAttributePtr attrDecl; |
| 4055 | if (doc->intSubset != NULL) { |
| 4056 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4057 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4058 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4059 | |
| 4060 | if (attrDecl->prefix != NULL) { |
| 4061 | /* |
| 4062 | * The DTD declaration only allows a prefix search |
| 4063 | */ |
| 4064 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4065 | if ((ns != NULL) && (xmlStrEqual(ns->href, namespace))) |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4066 | return(xmlStrdup(attrDecl->defaultValue)); |
| 4067 | } |
| 4068 | } |
| 4069 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4070 | return(NULL); |
| 4071 | } |
| 4072 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4073 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4074 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4075 | * @node: the node |
| 4076 | * @name: the attribute name |
| 4077 | * @value: the attribute value |
| 4078 | * |
| 4079 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4080 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4081 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4082 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4083 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4084 | xmlAttrPtr prop = node->properties; |
| 4085 | |
| 4086 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4087 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4088 | if (prop->children != NULL) |
| 4089 | xmlFreeNodeList(prop->children); |
| 4090 | prop->children = NULL; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4091 | prop->last = NULL; |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4092 | if (value != NULL) { |
| 4093 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4094 | xmlNodePtr tmp; |
| 4095 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4096 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4097 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4098 | prop->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4099 | tmp = prop->children; |
| 4100 | while (tmp != NULL) { |
| 4101 | tmp->parent = (xmlNodePtr) prop; |
| 4102 | if (tmp->next == NULL) |
| 4103 | prop->last = tmp; |
| 4104 | tmp = tmp->next; |
| 4105 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4106 | xmlFree(buffer); |
| 4107 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4108 | return(prop); |
| 4109 | } |
| 4110 | prop = prop->next; |
| 4111 | } |
| 4112 | prop = xmlNewProp(node, name, value); |
| 4113 | return(prop); |
| 4114 | } |
| 4115 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4116 | /** |
| 4117 | * xmlNodeIsText: |
| 4118 | * @node: the node |
| 4119 | * |
| 4120 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4121 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4122 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4123 | int |
| 4124 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4125 | if (node == NULL) return(0); |
| 4126 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4127 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4128 | return(0); |
| 4129 | } |
| 4130 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4131 | /** |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4132 | * xmlIsBlankNode: |
| 4133 | * @node: the node |
| 4134 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4135 | * Checks whether this node is an empty or whitespace only |
| 4136 | * (and possibly ignorable) text-node. |
| 4137 | * |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4138 | * Returns 1 yes, 0 no |
| 4139 | */ |
| 4140 | int |
| 4141 | xmlIsBlankNode(xmlNodePtr node) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4142 | const xmlChar *cur; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4143 | if (node == NULL) return(0); |
| 4144 | |
| 4145 | if (node->type != XML_TEXT_NODE) return(0); |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 4146 | if (node->content == NULL) return(1); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4147 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4148 | cur = node->content; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4149 | #else |
| 4150 | cur = xmlBufferContent(node->content); |
| 4151 | #endif |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4152 | while (*cur != 0) { |
| 4153 | if (!IS_BLANK(*cur)) return(0); |
| 4154 | cur++; |
| 4155 | } |
| 4156 | |
| 4157 | return(1); |
| 4158 | } |
| 4159 | |
| 4160 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4161 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4162 | * @node: the node |
| 4163 | * @content: the content |
| 4164 | * @len: @content lenght |
| 4165 | * |
| 4166 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4167 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4168 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4169 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4170 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4171 | if (node == NULL) return; |
| 4172 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4173 | if ((node->type != XML_TEXT_NODE) && |
| 4174 | (node->type != XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4175 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4176 | xmlGenericError(xmlGenericErrorContext, |
| 4177 | "xmlTextConcat: node is not text nor cdata\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4178 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4179 | return; |
| 4180 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4181 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4182 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4183 | #else |
| 4184 | xmlBufferAdd(node->content, content, len); |
| 4185 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | /************************************************************************ |
| 4189 | * * |
| 4190 | * Output : to a FILE or in memory * |
| 4191 | * * |
| 4192 | ************************************************************************/ |
| 4193 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4194 | #define BASE_BUFFER_SIZE 4000 |
| 4195 | |
| 4196 | /** |
| 4197 | * xmlBufferCreate: |
| 4198 | * |
| 4199 | * routine to create an XML buffer. |
| 4200 | * returns the new structure. |
| 4201 | */ |
| 4202 | xmlBufferPtr |
| 4203 | xmlBufferCreate(void) { |
| 4204 | xmlBufferPtr ret; |
| 4205 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4206 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4207 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4208 | xmlGenericError(xmlGenericErrorContext, |
| 4209 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4210 | return(NULL); |
| 4211 | } |
| 4212 | ret->use = 0; |
| 4213 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4214 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4215 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4216 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4217 | xmlGenericError(xmlGenericErrorContext, |
| 4218 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4219 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4220 | return(NULL); |
| 4221 | } |
| 4222 | ret->content[0] = 0; |
| 4223 | return(ret); |
| 4224 | } |
| 4225 | |
| 4226 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4227 | * xmlBufferCreateSize: |
| 4228 | * @size: initial size of buffer |
| 4229 | * |
| 4230 | * routine to create an XML buffer. |
| 4231 | * returns the new structure. |
| 4232 | */ |
| 4233 | xmlBufferPtr |
| 4234 | xmlBufferCreateSize(size_t size) { |
| 4235 | xmlBufferPtr ret; |
| 4236 | |
| 4237 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 4238 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4239 | xmlGenericError(xmlGenericErrorContext, |
| 4240 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4241 | return(NULL); |
| 4242 | } |
| 4243 | ret->use = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4244 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4245 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4246 | if (ret->size){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4247 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 4248 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4249 | xmlGenericError(xmlGenericErrorContext, |
| 4250 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4251 | xmlFree(ret); |
| 4252 | return(NULL); |
| 4253 | } |
| 4254 | ret->content[0] = 0; |
| 4255 | } else |
| 4256 | ret->content = NULL; |
| 4257 | return(ret); |
| 4258 | } |
| 4259 | |
| 4260 | /** |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 4261 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4262 | * @buf: the buffer to free |
| 4263 | * @scheme: allocation scheme to use |
| 4264 | * |
| 4265 | * Sets the allocation scheme for this buffer |
| 4266 | */ |
| 4267 | void |
| 4268 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 4269 | xmlBufferAllocationScheme scheme) { |
| 4270 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4271 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4272 | xmlGenericError(xmlGenericErrorContext, |
| 4273 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4274 | #endif |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4275 | return; |
| 4276 | } |
| 4277 | |
| 4278 | buf->alloc = scheme; |
| 4279 | } |
| 4280 | |
| 4281 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4282 | * xmlBufferFree: |
| 4283 | * @buf: the buffer to free |
| 4284 | * |
| 4285 | * Frees an XML buffer. |
| 4286 | */ |
| 4287 | void |
| 4288 | xmlBufferFree(xmlBufferPtr buf) { |
| 4289 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4290 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4291 | xmlGenericError(xmlGenericErrorContext, |
| 4292 | "xmlBufferFree: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4293 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4294 | return; |
| 4295 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4296 | if (buf->content != NULL) { |
| 4297 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4298 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4299 | #else |
| 4300 | memset(buf->content, -1, buf->size); |
| 4301 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4302 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4303 | } |
| 4304 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4305 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4309 | * xmlBufferEmpty: |
| 4310 | * @buf: the buffer |
| 4311 | * |
| 4312 | * empty a buffer. |
| 4313 | */ |
| 4314 | void |
| 4315 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | 4fb87ee | 2000-09-19 12:25:59 +0000 | [diff] [blame] | 4316 | if (buf->content == NULL) return; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4317 | buf->use = 0; |
| 4318 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 4319 | } |
| 4320 | |
| 4321 | /** |
| 4322 | * xmlBufferShrink: |
| 4323 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4324 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4325 | * |
| 4326 | * Remove the beginning of an XML buffer. |
| 4327 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4328 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4329 | */ |
| 4330 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4331 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4332 | if (len == 0) return(0); |
| 4333 | if (len > buf->use) return(-1); |
| 4334 | |
| 4335 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4336 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4337 | |
| 4338 | buf->content[buf->use] = 0; |
| 4339 | return(len); |
| 4340 | } |
| 4341 | |
| 4342 | /** |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4343 | * xmlBufferGrow: |
| 4344 | * @buf: the buffer |
| 4345 | * @len: the minimum free sie to allocate |
| 4346 | * |
| 4347 | * Grow the available space of an XML buffer. |
| 4348 | * |
| 4349 | * Returns the new available space or -1 in case of error |
| 4350 | */ |
| 4351 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4352 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4353 | int size; |
| 4354 | xmlChar *newbuf; |
| 4355 | |
| 4356 | if (len <= buf->use) return(0); |
| 4357 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4358 | size = buf->use + len + 100; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4359 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4360 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4361 | if (newbuf == NULL) return(-1); |
| 4362 | buf->content = newbuf; |
| 4363 | buf->size = size; |
| 4364 | return(buf->size - buf->use); |
| 4365 | } |
| 4366 | |
| 4367 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4368 | * xmlBufferDump: |
| 4369 | * @file: the file output |
| 4370 | * @buf: the buffer to dump |
| 4371 | * |
| 4372 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4373 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4374 | */ |
| 4375 | int |
| 4376 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 4377 | int ret; |
| 4378 | |
| 4379 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4380 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4381 | xmlGenericError(xmlGenericErrorContext, |
| 4382 | "xmlBufferDump: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4383 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4384 | return(0); |
| 4385 | } |
| 4386 | if (buf->content == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4387 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4388 | xmlGenericError(xmlGenericErrorContext, |
| 4389 | "xmlBufferDump: buf->content == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4390 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4391 | return(0); |
| 4392 | } |
| 4393 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4394 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4395 | return(ret); |
| 4396 | } |
| 4397 | |
| 4398 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4399 | * xmlBufferContent: |
| 4400 | * @buf: the buffer to resize |
| 4401 | * |
| 4402 | * Returns the internal content |
| 4403 | */ |
| 4404 | |
| 4405 | const xmlChar* |
| 4406 | xmlBufferContent(const xmlBufferPtr buf) |
| 4407 | { |
| 4408 | if(!buf) |
| 4409 | return NULL; |
| 4410 | |
| 4411 | return buf->content; |
| 4412 | } |
| 4413 | |
| 4414 | /** |
| 4415 | * xmlBufferLength: |
| 4416 | * @buf: the buffer |
| 4417 | * |
| 4418 | * Returns the length of data in the internal content |
| 4419 | */ |
| 4420 | |
| 4421 | int |
| 4422 | xmlBufferLength(const xmlBufferPtr buf) |
| 4423 | { |
| 4424 | if(!buf) |
| 4425 | return 0; |
| 4426 | |
| 4427 | return buf->use; |
| 4428 | } |
| 4429 | |
| 4430 | /** |
| 4431 | * xmlBufferResize: |
| 4432 | * @buf: the buffer to resize |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4433 | * @size: the desired size |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4434 | * |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4435 | * Resize a buffer to accomodate minimum size of @size. |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4436 | * |
| 4437 | * Returns 0 in case of problems, 1 otherwise |
| 4438 | */ |
| 4439 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4440 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4441 | { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4442 | unsigned int newSize; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4443 | xmlChar* rebuf = NULL; |
| 4444 | |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4445 | /*take care of empty case*/ |
| 4446 | newSize = (buf->size ? buf->size*2 : size); |
| 4447 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4448 | /* Don't resize if we don't have to */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4449 | if (size < buf->size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4450 | return 1; |
| 4451 | |
| 4452 | /* figure out new size */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4453 | switch (buf->alloc){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4454 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4455 | while (size > newSize) newSize *= 2; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4456 | break; |
| 4457 | case XML_BUFFER_ALLOC_EXACT: |
| 4458 | newSize = size+10; |
| 4459 | break; |
| 4460 | default: |
| 4461 | newSize = size+10; |
| 4462 | break; |
| 4463 | } |
| 4464 | |
| 4465 | if (buf->content == NULL) |
| 4466 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 4467 | else |
| 4468 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 4469 | newSize * sizeof(xmlChar)); |
| 4470 | if (rebuf == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4471 | xmlGenericError(xmlGenericErrorContext, |
| 4472 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4473 | return 0; |
| 4474 | } |
| 4475 | buf->content = rebuf; |
| 4476 | buf->size = newSize; |
| 4477 | |
| 4478 | return 1; |
| 4479 | } |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4480 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4481 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4482 | * xmlBufferAdd: |
| 4483 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4484 | * @str: the xmlChar string |
| 4485 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4486 | * |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4487 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 4488 | * str is recomputed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4489 | */ |
| 4490 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4491 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4492 | unsigned int needSize; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4493 | |
| 4494 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4495 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4496 | xmlGenericError(xmlGenericErrorContext, |
| 4497 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4498 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4499 | return; |
| 4500 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4501 | if (len < -1) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4502 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4503 | xmlGenericError(xmlGenericErrorContext, |
| 4504 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4505 | #endif |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4506 | return; |
| 4507 | } |
| 4508 | if (len == 0) return; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4509 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4510 | if (len < 0) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4511 | len = xmlStrlen(str); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4512 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4513 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4514 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4515 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4516 | if (needSize > buf->size){ |
| 4517 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4518 | xmlGenericError(xmlGenericErrorContext, |
| 4519 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4520 | return; |
| 4521 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4522 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4523 | |
| 4524 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4525 | buf->use += len; |
| 4526 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4530 | * xmlBufferAddHead: |
| 4531 | * @buf: the buffer |
| 4532 | * @str: the xmlChar string |
| 4533 | * @len: the number of xmlChar to add |
| 4534 | * |
| 4535 | * Add a string range to the beginning of an XML buffer. |
| 4536 | * if len == -1, the lenght of @str is recomputed. |
| 4537 | */ |
| 4538 | void |
| 4539 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4540 | unsigned int needSize; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4541 | |
| 4542 | if (str == NULL) { |
| 4543 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4544 | xmlGenericError(xmlGenericErrorContext, |
| 4545 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4546 | #endif |
| 4547 | return; |
| 4548 | } |
| 4549 | if (len < -1) { |
| 4550 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4551 | xmlGenericError(xmlGenericErrorContext, |
| 4552 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4553 | #endif |
| 4554 | return; |
| 4555 | } |
| 4556 | if (len == 0) return; |
| 4557 | |
| 4558 | if (len < 0) |
| 4559 | len = xmlStrlen(str); |
| 4560 | |
| 4561 | if (len <= 0) return; |
| 4562 | |
| 4563 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4564 | if (needSize > buf->size){ |
| 4565 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4566 | xmlGenericError(xmlGenericErrorContext, |
| 4567 | "xmlBufferAddHead : out of memory!\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4568 | return; |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 4573 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 4574 | buf->use += len; |
| 4575 | buf->content[buf->use] = 0; |
| 4576 | } |
| 4577 | |
| 4578 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4579 | * xmlBufferCat: |
| 4580 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4581 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4582 | * |
| 4583 | * Append a zero terminated string to an XML buffer. |
| 4584 | */ |
| 4585 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4586 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4587 | if (str != NULL) |
| 4588 | xmlBufferAdd(buf, str, -1); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | /** |
| 4592 | * xmlBufferCCat: |
| 4593 | * @buf: the buffer to dump |
| 4594 | * @str: the C char string |
| 4595 | * |
| 4596 | * Append a zero terminated C string to an XML buffer. |
| 4597 | */ |
| 4598 | void |
| 4599 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 4600 | const char *cur; |
| 4601 | |
| 4602 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4603 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4604 | xmlGenericError(xmlGenericErrorContext, |
| 4605 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4606 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4607 | return; |
| 4608 | } |
| 4609 | for (cur = str;*cur != 0;cur++) { |
| 4610 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4611 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4612 | xmlGenericError(xmlGenericErrorContext, |
| 4613 | "xmlBufferCCat : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4614 | return; |
| 4615 | } |
| 4616 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4617 | buf->content[buf->use++] = *cur; |
| 4618 | } |
| 4619 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4620 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4621 | /** |
| 4622 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4623 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4624 | * @string: the string to add |
| 4625 | * |
| 4626 | * routine which manage and grows an output buffer. This one add |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4627 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4628 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4629 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4630 | xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4631 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4634 | /** |
| 4635 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4636 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4637 | * @string: the string to add |
| 4638 | * |
| 4639 | * routine which manage and grows an output buffer. This one add |
| 4640 | * C chars at the end of the array. |
| 4641 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4642 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4643 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 4644 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4647 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4648 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4649 | * xmlBufferWriteQuotedString: |
| 4650 | * @buf: the XML buffer output |
| 4651 | * @string: the string to add |
| 4652 | * |
| 4653 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4654 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4655 | * quote or double-quotes internally |
| 4656 | */ |
| 4657 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4658 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4659 | if (xmlStrchr(string, '"')) { |
| 4660 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4661 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4662 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4663 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4664 | #endif |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4665 | } |
| 4666 | xmlBufferCCat(buf, "'"); |
| 4667 | xmlBufferCat(buf, string); |
| 4668 | xmlBufferCCat(buf, "'"); |
| 4669 | } else { |
| 4670 | xmlBufferCCat(buf, "\""); |
| 4671 | xmlBufferCat(buf, string); |
| 4672 | xmlBufferCCat(buf, "\""); |
| 4673 | } |
| 4674 | } |
| 4675 | |
| 4676 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4677 | /************************************************************************ |
| 4678 | * * |
| 4679 | * Dumping XML tree content to a simple buffer * |
| 4680 | * * |
| 4681 | ************************************************************************/ |
| 4682 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4683 | void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4684 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4685 | int format); |
| 4686 | static void |
| 4687 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4688 | int format); |
| 4689 | void |
| 4690 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 4691 | |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4692 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4693 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4694 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4695 | * @cur: a namespace |
| 4696 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4697 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4698 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4699 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4700 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4701 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4702 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4703 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4704 | xmlGenericError(xmlGenericErrorContext, |
| 4705 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4706 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4707 | return; |
| 4708 | } |
| 4709 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4710 | /* Within the context of an element attributes */ |
| 4711 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4712 | xmlBufferWriteChar(buf, " xmlns:"); |
| 4713 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4714 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4715 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4716 | xmlBufferWriteChar(buf, "="); |
| 4717 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4721 | /** |
| 4722 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4723 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4724 | * @cur: the first namespace |
| 4725 | * |
| 4726 | * Dump a list of local Namespace definitions. |
| 4727 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4728 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4729 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4730 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4731 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4732 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4733 | cur = cur->next; |
| 4734 | } |
| 4735 | } |
| 4736 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4737 | /** |
| 4738 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4739 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4740 | * @doc: the document |
| 4741 | * |
| 4742 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4743 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4744 | static void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4745 | xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) { |
| 4746 | if (dtd == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4747 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4748 | xmlGenericError(xmlGenericErrorContext, |
| 4749 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4750 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4751 | return; |
| 4752 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4753 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4754 | xmlBufferWriteCHAR(buf, dtd->name); |
| 4755 | if (dtd->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4756 | xmlBufferWriteChar(buf, " PUBLIC "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4757 | xmlBufferWriteQuotedString(buf, dtd->ExternalID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4758 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4759 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4760 | } else if (dtd->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4761 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4762 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4763 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4764 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4765 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4766 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4767 | return; |
| 4768 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4769 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4770 | xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0); |
| 4771 | #if 0 |
| 4772 | if (dtd->entities != NULL) |
| 4773 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4774 | if (dtd->notations != NULL) |
| 4775 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4776 | if (dtd->elements != NULL) |
| 4777 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 4778 | if (dtd->attributes != NULL) |
| 4779 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 4780 | #endif |
| 4781 | xmlBufferWriteChar(buf, "]>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4784 | /** |
| 4785 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4786 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4787 | * @doc: the document |
| 4788 | * @cur: the attribute pointer |
| 4789 | * |
| 4790 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4791 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4792 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4793 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4794 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4795 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4796 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4797 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4798 | xmlGenericError(xmlGenericErrorContext, |
| 4799 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4800 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4801 | return; |
| 4802 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4803 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4804 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 4805 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4806 | xmlBufferWriteChar(buf, ":"); |
| 4807 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4808 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4809 | value = xmlNodeListGetString(doc, cur->children, 0); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4810 | if (value) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4811 | xmlBufferWriteChar(buf, "="); |
| 4812 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4813 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 4814 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4815 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4816 | } |
| 4817 | } |
| 4818 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4819 | /** |
| 4820 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4821 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4822 | * @doc: the document |
| 4823 | * @cur: the first attribute pointer |
| 4824 | * |
| 4825 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4826 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4827 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4828 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4829 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4830 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4831 | xmlGenericError(xmlGenericErrorContext, |
| 4832 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4833 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4834 | return; |
| 4835 | } |
| 4836 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4837 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4838 | cur = cur->next; |
| 4839 | } |
| 4840 | } |
| 4841 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4842 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4843 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4844 | /** |
| 4845 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4846 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4847 | * @doc: the document |
| 4848 | * @cur: the first node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4849 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4850 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4851 | * |
| 4852 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 4853 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4854 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4855 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4856 | int format) { |
| 4857 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4858 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4859 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4860 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4861 | xmlGenericError(xmlGenericErrorContext, |
| 4862 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4863 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4864 | return; |
| 4865 | } |
| 4866 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4867 | if ((format) && (xmlIndentTreeOutput) && |
| 4868 | (cur->type == XML_ELEMENT_NODE)) |
| 4869 | for (i = 0;i < level;i++) |
| 4870 | xmlBufferWriteChar(buf, " "); |
| 4871 | xmlNodeDump(buf, doc, cur, level, format); |
| 4872 | if (format) { |
| 4873 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4874 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4875 | cur = cur->next; |
| 4876 | } |
| 4877 | } |
| 4878 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4879 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4880 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4881 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4882 | * @doc: the document |
| 4883 | * @cur: the current node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4884 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4885 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4886 | * |
| 4887 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4888 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4889 | void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4890 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4891 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4892 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4893 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4894 | |
| 4895 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4896 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4897 | xmlGenericError(xmlGenericErrorContext, |
| 4898 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4899 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4900 | return; |
| 4901 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4902 | if (cur->type == XML_XINCLUDE_START) |
| 4903 | return; |
| 4904 | if (cur->type == XML_XINCLUDE_END) |
| 4905 | return; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4906 | if (cur->type == XML_DTD_NODE) { |
| 4907 | xmlDtdDump(buf, (xmlDtdPtr) cur); |
| 4908 | return; |
| 4909 | } |
| 4910 | if (cur->type == XML_ELEMENT_DECL) { |
| 4911 | xmlDumpElementDecl(buf, (xmlElementPtr) cur); |
| 4912 | return; |
| 4913 | } |
| 4914 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 4915 | xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur); |
| 4916 | return; |
| 4917 | } |
| 4918 | if (cur->type == XML_ENTITY_DECL) { |
| 4919 | xmlDumpEntityDecl(buf, (xmlEntityPtr) cur); |
| 4920 | return; |
| 4921 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4922 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4923 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4924 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4925 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4926 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4927 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4928 | #else |
| 4929 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 4930 | xmlBufferContent(cur->content)); |
| 4931 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4932 | if (buffer != NULL) { |
| 4933 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4934 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 4935 | } |
| 4936 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4937 | return; |
| 4938 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4939 | if (cur->type == XML_PI_NODE) { |
| 4940 | if (cur->content != NULL) { |
| 4941 | xmlBufferWriteChar(buf, "<?"); |
| 4942 | xmlBufferWriteCHAR(buf, cur->name); |
| 4943 | if (cur->content != NULL) { |
| 4944 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4945 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4946 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4947 | #else |
| 4948 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4949 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4950 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4951 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4952 | } else { |
| 4953 | xmlBufferWriteChar(buf, "<?"); |
| 4954 | xmlBufferWriteCHAR(buf, cur->name); |
| 4955 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4956 | } |
| 4957 | return; |
| 4958 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4959 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4960 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4961 | xmlBufferWriteChar(buf, "<!--"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4962 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4963 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4964 | #else |
| 4965 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4966 | #endif |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4967 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4968 | } |
| 4969 | return; |
| 4970 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4971 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4972 | xmlBufferWriteChar(buf, "&"); |
| 4973 | xmlBufferWriteCHAR(buf, cur->name); |
| 4974 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4975 | return; |
| 4976 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4977 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 4978 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 4979 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4980 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4981 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4982 | #else |
| 4983 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 4984 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 4985 | xmlBufferWriteChar(buf, "]]>"); |
| 4986 | return; |
| 4987 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4988 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4989 | if (format == 1) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4990 | tmp = cur->children; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4991 | while (tmp != NULL) { |
| 4992 | if ((tmp->type == XML_TEXT_NODE) || |
| 4993 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 4994 | format = 0; |
| 4995 | break; |
| 4996 | } |
| 4997 | tmp = tmp->next; |
| 4998 | } |
| 4999 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5000 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5001 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5002 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5003 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5004 | } |
| 5005 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5006 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5007 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5008 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5009 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5010 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5011 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5012 | if ((cur->content == NULL) && (cur->children == NULL) && |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 5013 | (!xmlSaveNoEmptyTags)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5014 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5015 | return; |
| 5016 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5017 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5018 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5019 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5020 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5021 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5022 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5023 | #else |
| 5024 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5025 | xmlBufferContent(cur->content)); |
| 5026 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5027 | if (buffer != NULL) { |
| 5028 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 5029 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5030 | } |
| 5031 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5032 | if (cur->children != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5033 | if (format) xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5034 | xmlNodeListDump(buf, doc, cur->children, |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 5035 | (level >= 0?level+1:-1), format); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5036 | if ((xmlIndentTreeOutput) && (format)) |
| 5037 | for (i = 0;i < level;i++) |
| 5038 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5039 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5040 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5041 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5042 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5043 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5044 | } |
| 5045 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5046 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5047 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5050 | /** |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5051 | * xmlElemDump: |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 5052 | * @f: the FILE * for the output |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5053 | * @doc: the document |
| 5054 | * @cur: the current node |
| 5055 | * |
| 5056 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 5057 | */ |
| 5058 | void |
| 5059 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 5060 | xmlBufferPtr buf; |
| 5061 | |
| 5062 | if (cur == NULL) { |
| 5063 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5064 | xmlGenericError(xmlGenericErrorContext, |
| 5065 | "xmlElemDump : cur == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5066 | #endif |
| 5067 | return; |
| 5068 | } |
| 5069 | if (doc == NULL) { |
| 5070 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5071 | xmlGenericError(xmlGenericErrorContext, |
| 5072 | "xmlElemDump : doc == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5073 | #endif |
| 5074 | } |
| 5075 | buf = xmlBufferCreate(); |
| 5076 | if (buf == NULL) return; |
| 5077 | if ((doc != NULL) && |
| 5078 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5079 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5080 | htmlNodeDump(buf, doc, cur); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5081 | #else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5082 | xmlGenericError(xmlGenericErrorContext, |
| 5083 | "HTML support not compiled in\n"); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5084 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5085 | } else |
| 5086 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 5087 | xmlBufferDump(f, buf); |
| 5088 | xmlBufferFree(buf); |
| 5089 | } |
| 5090 | |
| 5091 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5092 | * xmlDocContentDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 5093 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5094 | * @cur: the document |
| 5095 | * |
| 5096 | * Dump an XML document. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5097 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5098 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5099 | xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { |
Daniel Veillard | be70ff7 | 1999-07-05 16:50:46 +0000 | [diff] [blame] | 5100 | xmlBufferWriteChar(buf, "<?xml version="); |
| 5101 | if (cur->version != NULL) |
| 5102 | xmlBufferWriteQuotedString(buf, cur->version); |
| 5103 | else |
| 5104 | xmlBufferWriteChar(buf, "\"1.0\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5105 | if (cur->encoding != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 5106 | xmlBufferWriteChar(buf, " encoding="); |
| 5107 | xmlBufferWriteQuotedString(buf, cur->encoding); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5108 | } |
| 5109 | switch (cur->standalone) { |
| 5110 | case 0: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5111 | xmlBufferWriteChar(buf, " standalone=\"no\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5112 | break; |
| 5113 | case 1: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5114 | xmlBufferWriteChar(buf, " standalone=\"yes\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5115 | break; |
| 5116 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5117 | xmlBufferWriteChar(buf, "?>\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5118 | if (cur->children != NULL) { |
| 5119 | xmlNodePtr child = cur->children; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5120 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5121 | while (child != NULL) { |
Daniel Veillard | bf43275 | 2000-11-12 15:56:56 +0000 | [diff] [blame] | 5122 | xmlNodeDump(buf, cur, child, 0, 0); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5123 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5124 | child = child->next; |
| 5125 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5126 | } |
| 5127 | } |
| 5128 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5129 | /************************************************************************ |
| 5130 | * * |
| 5131 | * Dumping XML tree content to an I/O output buffer * |
| 5132 | * * |
| 5133 | ************************************************************************/ |
| 5134 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5135 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5136 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5137 | int level, int format, const char *encoding); |
| 5138 | static void |
| 5139 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5140 | int level, int format, const char *encoding); |
| 5141 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5142 | * xmlNsDumpOutput: |
| 5143 | * @buf: the XML buffer output |
| 5144 | * @cur: a namespace |
| 5145 | * |
| 5146 | * Dump a local Namespace definition. |
| 5147 | * Should be called in the context of attributes dumps. |
| 5148 | */ |
| 5149 | static void |
| 5150 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5151 | if (cur == NULL) { |
| 5152 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5153 | xmlGenericError(xmlGenericErrorContext, |
| 5154 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5155 | #endif |
| 5156 | return; |
| 5157 | } |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 5158 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5159 | /* Within the context of an element attributes */ |
| 5160 | if (cur->prefix != NULL) { |
| 5161 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 5162 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 5163 | } else |
| 5164 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 5165 | xmlOutputBufferWriteString(buf, "="); |
| 5166 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | /** |
| 5171 | * xmlNsListDumpOutput: |
| 5172 | * @buf: the XML buffer output |
| 5173 | * @cur: the first namespace |
| 5174 | * |
| 5175 | * Dump a list of local Namespace definitions. |
| 5176 | * Should be called in the context of attributes dumps. |
| 5177 | */ |
| 5178 | static void |
| 5179 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5180 | while (cur != NULL) { |
| 5181 | xmlNsDumpOutput(buf, cur); |
| 5182 | cur = cur->next; |
| 5183 | } |
| 5184 | } |
| 5185 | |
| 5186 | /** |
| 5187 | * xmlDtdDumpOutput: |
| 5188 | * @buf: the XML buffer output |
| 5189 | * @doc: the document |
| 5190 | * @encoding: an optional encoding string |
| 5191 | * |
| 5192 | * Dump the XML document DTD, if any. |
| 5193 | */ |
| 5194 | static void |
| 5195 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 5196 | if (dtd == NULL) { |
| 5197 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5198 | xmlGenericError(xmlGenericErrorContext, |
| 5199 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5200 | #endif |
| 5201 | return; |
| 5202 | } |
| 5203 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 5204 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 5205 | if (dtd->ExternalID != NULL) { |
| 5206 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 5207 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 5208 | xmlOutputBufferWriteString(buf, " "); |
| 5209 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5210 | } else if (dtd->SystemID != NULL) { |
| 5211 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 5212 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5213 | } |
| 5214 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 5215 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 5216 | xmlOutputBufferWriteString(buf, ">"); |
| 5217 | return; |
| 5218 | } |
| 5219 | xmlOutputBufferWriteString(buf, " [\n"); |
| 5220 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5221 | xmlOutputBufferWriteString(buf, "]>"); |
| 5222 | } |
| 5223 | |
| 5224 | /** |
| 5225 | * xmlAttrDumpOutput: |
| 5226 | * @buf: the XML buffer output |
| 5227 | * @doc: the document |
| 5228 | * @cur: the attribute pointer |
| 5229 | * @encoding: an optional encoding string |
| 5230 | * |
| 5231 | * Dump an XML attribute |
| 5232 | */ |
| 5233 | static void |
| 5234 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
| 5235 | const char *encoding) { |
| 5236 | xmlChar *value; |
| 5237 | |
| 5238 | if (cur == NULL) { |
| 5239 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5240 | xmlGenericError(xmlGenericErrorContext, |
| 5241 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5242 | #endif |
| 5243 | return; |
| 5244 | } |
| 5245 | xmlOutputBufferWriteString(buf, " "); |
| 5246 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5247 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5248 | xmlOutputBufferWriteString(buf, ":"); |
| 5249 | } |
| 5250 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5251 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 5252 | if (value) { |
| 5253 | xmlOutputBufferWriteString(buf, "="); |
| 5254 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 5255 | xmlFree(value); |
| 5256 | } else { |
| 5257 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | /** |
| 5262 | * xmlAttrListDumpOutput: |
| 5263 | * @buf: the XML buffer output |
| 5264 | * @doc: the document |
| 5265 | * @cur: the first attribute pointer |
| 5266 | * @encoding: an optional encoding string |
| 5267 | * |
| 5268 | * Dump a list of XML attributes |
| 5269 | */ |
| 5270 | static void |
| 5271 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5272 | xmlAttrPtr cur, const char *encoding) { |
| 5273 | if (cur == NULL) { |
| 5274 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5275 | xmlGenericError(xmlGenericErrorContext, |
| 5276 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5277 | #endif |
| 5278 | return; |
| 5279 | } |
| 5280 | while (cur != NULL) { |
| 5281 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 5282 | cur = cur->next; |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | |
| 5287 | |
| 5288 | /** |
| 5289 | * xmlNodeListDumpOutput: |
| 5290 | * @buf: the XML buffer output |
| 5291 | * @doc: the document |
| 5292 | * @cur: the first node |
| 5293 | * @level: the imbrication level for indenting |
| 5294 | * @format: is formatting allowed |
| 5295 | * @encoding: an optional encoding string |
| 5296 | * |
| 5297 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 5298 | */ |
| 5299 | static void |
| 5300 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5301 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 5302 | int i; |
| 5303 | |
| 5304 | if (cur == NULL) { |
| 5305 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5306 | xmlGenericError(xmlGenericErrorContext, |
| 5307 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5308 | #endif |
| 5309 | return; |
| 5310 | } |
| 5311 | while (cur != NULL) { |
| 5312 | if ((format) && (xmlIndentTreeOutput) && |
| 5313 | (cur->type == XML_ELEMENT_NODE)) |
| 5314 | for (i = 0;i < level;i++) |
| 5315 | xmlOutputBufferWriteString(buf, " "); |
| 5316 | xmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 5317 | if (format) { |
| 5318 | xmlOutputBufferWriteString(buf, "\n"); |
| 5319 | } |
| 5320 | cur = cur->next; |
| 5321 | } |
| 5322 | } |
| 5323 | |
| 5324 | /** |
| 5325 | * xmlNodeDumpOutput: |
| 5326 | * @buf: the XML buffer output |
| 5327 | * @doc: the document |
| 5328 | * @cur: the current node |
| 5329 | * @level: the imbrication level for indenting |
| 5330 | * @format: is formatting allowed |
| 5331 | * @encoding: an optional encoding string |
| 5332 | * |
| 5333 | * Dump an XML node, recursive behaviour,children are printed too. |
| 5334 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5335 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5336 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5337 | int level, int format, const char *encoding) { |
| 5338 | int i; |
| 5339 | xmlNodePtr tmp; |
| 5340 | |
| 5341 | if (cur == NULL) { |
| 5342 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5343 | xmlGenericError(xmlGenericErrorContext, |
| 5344 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5345 | #endif |
| 5346 | return; |
| 5347 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 5348 | if (cur->type == XML_XINCLUDE_START) |
| 5349 | return; |
| 5350 | if (cur->type == XML_XINCLUDE_END) |
| 5351 | return; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5352 | if (cur->type == XML_DTD_NODE) { |
| 5353 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 5354 | return; |
| 5355 | } |
| 5356 | if (cur->type == XML_ELEMENT_DECL) { |
| 5357 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 5358 | return; |
| 5359 | } |
| 5360 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5361 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 5362 | return; |
| 5363 | } |
| 5364 | if (cur->type == XML_ENTITY_DECL) { |
| 5365 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 5366 | return; |
| 5367 | } |
| 5368 | if (cur->type == XML_TEXT_NODE) { |
| 5369 | if (cur->content != NULL) { |
| 5370 | xmlChar *buffer; |
| 5371 | |
| 5372 | #ifndef XML_USE_BUFFER_CONTENT |
| 5373 | if (encoding == NULL) |
| 5374 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5375 | else |
| 5376 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5377 | #else |
| 5378 | if (encoding == NULL) |
| 5379 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5380 | xmlBufferContent(cur->content)); |
| 5381 | else |
| 5382 | buffer = xmlEncodeSpecialChars(doc, |
| 5383 | xmlBufferContent(cur->content)); |
| 5384 | #endif |
| 5385 | if (buffer != NULL) { |
| 5386 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5387 | xmlFree(buffer); |
| 5388 | } |
| 5389 | } |
| 5390 | return; |
| 5391 | } |
| 5392 | if (cur->type == XML_PI_NODE) { |
| 5393 | if (cur->content != NULL) { |
| 5394 | xmlOutputBufferWriteString(buf, "<?"); |
| 5395 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5396 | if (cur->content != NULL) { |
| 5397 | xmlOutputBufferWriteString(buf, " "); |
| 5398 | #ifndef XML_USE_BUFFER_CONTENT |
| 5399 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5400 | #else |
| 5401 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5402 | #endif |
| 5403 | } |
| 5404 | xmlOutputBufferWriteString(buf, "?>"); |
| 5405 | } else { |
| 5406 | xmlOutputBufferWriteString(buf, "<?"); |
| 5407 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5408 | xmlOutputBufferWriteString(buf, "?>"); |
| 5409 | } |
| 5410 | return; |
| 5411 | } |
| 5412 | if (cur->type == XML_COMMENT_NODE) { |
| 5413 | if (cur->content != NULL) { |
| 5414 | xmlOutputBufferWriteString(buf, "<!--"); |
| 5415 | #ifndef XML_USE_BUFFER_CONTENT |
| 5416 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5417 | #else |
| 5418 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5419 | #endif |
| 5420 | xmlOutputBufferWriteString(buf, "-->"); |
| 5421 | } |
| 5422 | return; |
| 5423 | } |
| 5424 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 5425 | xmlOutputBufferWriteString(buf, "&"); |
| 5426 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5427 | xmlOutputBufferWriteString(buf, ";"); |
| 5428 | return; |
| 5429 | } |
| 5430 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5431 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 5432 | if (cur->content != NULL) |
| 5433 | #ifndef XML_USE_BUFFER_CONTENT |
| 5434 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5435 | #else |
| 5436 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5437 | #endif |
| 5438 | xmlOutputBufferWriteString(buf, "]]>"); |
| 5439 | return; |
| 5440 | } |
| 5441 | |
| 5442 | if (format == 1) { |
| 5443 | tmp = cur->children; |
| 5444 | while (tmp != NULL) { |
| 5445 | if ((tmp->type == XML_TEXT_NODE) || |
| 5446 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5447 | format = 0; |
| 5448 | break; |
| 5449 | } |
| 5450 | tmp = tmp->next; |
| 5451 | } |
| 5452 | } |
| 5453 | xmlOutputBufferWriteString(buf, "<"); |
| 5454 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5455 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5456 | xmlOutputBufferWriteString(buf, ":"); |
| 5457 | } |
| 5458 | |
| 5459 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5460 | if (cur->nsDef) |
| 5461 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 5462 | if (cur->properties != NULL) |
| 5463 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 5464 | |
| 5465 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5466 | (!xmlSaveNoEmptyTags)) { |
| 5467 | xmlOutputBufferWriteString(buf, "/>"); |
| 5468 | return; |
| 5469 | } |
| 5470 | xmlOutputBufferWriteString(buf, ">"); |
| 5471 | if (cur->content != NULL) { |
| 5472 | xmlChar *buffer; |
| 5473 | |
| 5474 | #ifndef XML_USE_BUFFER_CONTENT |
| 5475 | if (encoding == NULL) |
| 5476 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5477 | else |
| 5478 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5479 | #else |
| 5480 | if (encoding == NULL) |
| 5481 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5482 | xmlBufferContent(cur->content)); |
| 5483 | else |
| 5484 | buffer = xmlEncodeSpecialChars(doc, |
| 5485 | xmlBufferContent(cur->content)); |
| 5486 | #endif |
| 5487 | if (buffer != NULL) { |
| 5488 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5489 | xmlFree(buffer); |
| 5490 | } |
| 5491 | } |
| 5492 | if (cur->children != NULL) { |
| 5493 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 5494 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 5495 | (level >= 0?level+1:-1), format, encoding); |
| 5496 | if ((xmlIndentTreeOutput) && (format)) |
| 5497 | for (i = 0;i < level;i++) |
| 5498 | xmlOutputBufferWriteString(buf, " "); |
| 5499 | } |
| 5500 | xmlOutputBufferWriteString(buf, "</"); |
| 5501 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5502 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5503 | xmlOutputBufferWriteString(buf, ":"); |
| 5504 | } |
| 5505 | |
| 5506 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5507 | xmlOutputBufferWriteString(buf, ">"); |
| 5508 | } |
| 5509 | |
| 5510 | /** |
| 5511 | * xmlDocContentDumpOutput: |
| 5512 | * @buf: the XML buffer output |
| 5513 | * @cur: the document |
| 5514 | * @encoding: an optional encoding string |
| 5515 | * |
| 5516 | * Dump an XML document. |
| 5517 | */ |
| 5518 | static void |
| 5519 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
| 5520 | const char *encoding) { |
| 5521 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 5522 | if (cur->version != NULL) |
| 5523 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 5524 | else |
| 5525 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 5526 | if (encoding == NULL) { |
| 5527 | if (cur->encoding != NULL) |
| 5528 | encoding = (const char *) cur->encoding; |
| 5529 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5530 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5531 | } |
| 5532 | if (encoding != NULL) { |
| 5533 | xmlOutputBufferWriteString(buf, " encoding="); |
| 5534 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 5535 | } |
| 5536 | switch (cur->standalone) { |
| 5537 | case 0: |
| 5538 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 5539 | break; |
| 5540 | case 1: |
| 5541 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 5542 | break; |
| 5543 | } |
| 5544 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 5545 | if (cur->children != NULL) { |
| 5546 | xmlNodePtr child = cur->children; |
| 5547 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5548 | while (child != NULL) { |
| 5549 | xmlNodeDumpOutput(buf, cur, child, 0, 1, encoding); |
| 5550 | xmlOutputBufferWriteString(buf, "\n"); |
| 5551 | child = child->next; |
| 5552 | } |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | /************************************************************************ |
| 5557 | * * |
| 5558 | * Saving functions front-ends * |
| 5559 | * * |
| 5560 | ************************************************************************/ |
| 5561 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5562 | /** |
| 5563 | * xmlDocDumpMemory: |
| 5564 | * @cur: the document |
| 5565 | * @mem: OUT: the memory pointer |
| 5566 | * @size: OUT: the memory lenght |
| 5567 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5568 | * 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] | 5569 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5570 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5571 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5572 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5573 | xmlBufferPtr buf; |
| 5574 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5575 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5576 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5577 | xmlGenericError(xmlGenericErrorContext, |
| 5578 | "xmlDocDumpMemory : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 5579 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5580 | *mem = NULL; |
| 5581 | *size = 0; |
| 5582 | return; |
| 5583 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5584 | buf = xmlBufferCreate(); |
| 5585 | if (buf == NULL) { |
| 5586 | *mem = NULL; |
| 5587 | *size = 0; |
| 5588 | return; |
| 5589 | } |
| 5590 | xmlDocContentDump(buf, cur); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5591 | *mem = xmlStrndup(buf->content, buf->use); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5592 | *size = buf->use; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5593 | xmlBufferFree(buf); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5596 | /** |
| 5597 | * xmlGetDocCompressMode: |
| 5598 | * @doc: the document |
| 5599 | * |
| 5600 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5601 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5602 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5603 | int |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5604 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5605 | if (doc == NULL) return(-1); |
| 5606 | return(doc->compression); |
| 5607 | } |
| 5608 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5609 | /** |
| 5610 | * xmlSetDocCompressMode: |
| 5611 | * @doc: the document |
| 5612 | * @mode: the compression ratio |
| 5613 | * |
| 5614 | * set the compression ratio for a document, ZLIB based |
| 5615 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5616 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5617 | void |
| 5618 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5619 | if (doc == NULL) return; |
| 5620 | if (mode < 0) doc->compression = 0; |
| 5621 | else if (mode > 9) doc->compression = 9; |
| 5622 | else doc->compression = mode; |
| 5623 | } |
| 5624 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5625 | /** |
| 5626 | * xmlGetCompressMode: |
| 5627 | * |
| 5628 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5629 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5630 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5631 | int |
| 5632 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5633 | return(xmlCompressMode); |
| 5634 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5635 | |
| 5636 | /** |
| 5637 | * xmlSetCompressMode: |
| 5638 | * @mode: the compression ratio |
| 5639 | * |
| 5640 | * set the default compression mode used, ZLIB based |
| 5641 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5642 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5643 | void |
| 5644 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5645 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5646 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5647 | else xmlCompressMode = mode; |
| 5648 | } |
| 5649 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5650 | /** |
| 5651 | * xmlDocDump: |
| 5652 | * @f: the FILE* |
| 5653 | * @cur: the document |
| 5654 | * |
| 5655 | * Dump an XML document to an open FILE. |
| 5656 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5657 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5658 | */ |
| 5659 | int |
| 5660 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 5661 | xmlOutputBufferPtr buf; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5662 | const char * encoding; |
| 5663 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5664 | int ret; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5665 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5666 | if (cur == NULL) { |
| 5667 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5668 | xmlGenericError(xmlGenericErrorContext, |
| 5669 | "xmlDocDump : document == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5670 | #endif |
| 5671 | return(-1); |
| 5672 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5673 | encoding = (const char *) cur->encoding; |
| 5674 | |
| 5675 | if (encoding != NULL) { |
| 5676 | xmlCharEncoding enc; |
| 5677 | |
| 5678 | enc = xmlParseCharEncoding(encoding); |
| 5679 | |
| 5680 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5681 | xmlGenericError(xmlGenericErrorContext, |
| 5682 | "xmlDocDump: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5683 | return(-1); |
| 5684 | } |
| 5685 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5686 | handler = xmlFindCharEncodingHandler(encoding); |
| 5687 | if (handler == NULL) { |
| 5688 | xmlFree((char *) cur->encoding); |
| 5689 | cur->encoding = NULL; |
| 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | buf = xmlOutputBufferCreateFile(f, handler); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5694 | if (buf == NULL) return(-1); |
| 5695 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5696 | |
| 5697 | ret = xmlOutputBufferClose(buf); |
| 5698 | return(ret); |
| 5699 | } |
| 5700 | |
| 5701 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5702 | * xmlSaveFileTo: |
| 5703 | * @buf: an output I/O buffer |
| 5704 | * @cur: the document |
| 5705 | * @encoding: the encoding if any assuming the i/O layer handles the trancoding |
| 5706 | * |
| 5707 | * Dump an XML document to an I/O buffer. |
| 5708 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5709 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5710 | */ |
| 5711 | int |
| 5712 | xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) { |
| 5713 | int ret; |
| 5714 | |
| 5715 | if (buf == NULL) return(0); |
| 5716 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5717 | ret = xmlOutputBufferClose(buf); |
| 5718 | return(ret); |
| 5719 | } |
| 5720 | |
| 5721 | /** |
| 5722 | * xmlSaveFileEnc: |
| 5723 | * @filename: the filename (or URL) |
| 5724 | * @cur: the document |
| 5725 | * @encoding: the name of an encoding (or NULL) |
| 5726 | * |
| 5727 | * Dump an XML document, converting it to the given encoding |
| 5728 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5729 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5730 | */ |
| 5731 | int |
| 5732 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 5733 | xmlOutputBufferPtr buf; |
| 5734 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5735 | int ret; |
| 5736 | |
| 5737 | if (encoding != NULL) { |
| 5738 | xmlCharEncoding enc; |
| 5739 | |
| 5740 | enc = xmlParseCharEncoding(encoding); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5741 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5742 | xmlGenericError(xmlGenericErrorContext, |
| 5743 | "xmlSaveFileEnc: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5744 | return(-1); |
| 5745 | } |
| 5746 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5747 | handler = xmlFindCharEncodingHandler(encoding); |
| 5748 | if (handler == NULL) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5749 | return(-1); |
| 5750 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5751 | } |
| 5752 | } |
| 5753 | |
| 5754 | /* |
| 5755 | * save the content to a temp buffer. |
| 5756 | */ |
| 5757 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
| 5758 | if (buf == NULL) return(0); |
| 5759 | |
| 5760 | xmlDocContentDumpOutput(buf, cur, encoding); |
| 5761 | |
| 5762 | ret = xmlOutputBufferClose(buf); |
| 5763 | return(ret); |
| 5764 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5765 | |
| 5766 | /** |
| 5767 | * xmlSaveFile: |
| 5768 | * @filename: the filename (or URL) |
| 5769 | * @cur: the document |
| 5770 | * |
| 5771 | * Dump an XML document to a file. Will use compression if |
| 5772 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 5773 | * used. |
| 5774 | * returns: the number of byte written or -1 in case of failure. |
| 5775 | */ |
| 5776 | int |
| 5777 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 5778 | xmlOutputBufferPtr buf; |
| 5779 | const char *encoding; |
| 5780 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5781 | int ret; |
| 5782 | |
| 5783 | if (cur == NULL) |
| 5784 | return(-1); |
| 5785 | encoding = (const char *) cur->encoding; |
| 5786 | |
| 5787 | /* |
| 5788 | * save the content to a temp buffer. |
| 5789 | */ |
| 5790 | #ifdef HAVE_ZLIB_H |
| 5791 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5792 | #endif |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5793 | if (encoding != NULL) { |
| 5794 | xmlCharEncoding enc; |
| 5795 | |
| 5796 | enc = xmlParseCharEncoding(encoding); |
| 5797 | |
| 5798 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5799 | xmlGenericError(xmlGenericErrorContext, |
| 5800 | "xmlSaveFile: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5801 | return(-1); |
| 5802 | } |
| 5803 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5804 | handler = xmlFindCharEncodingHandler(encoding); |
| 5805 | if (handler == NULL) { |
| 5806 | xmlFree((char *) cur->encoding); |
| 5807 | cur->encoding = NULL; |
| 5808 | } |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
| 5813 | if (buf == NULL) return(0); |
| 5814 | |
| 5815 | xmlDocContentDumpOutput(buf, cur, NULL); |
| 5816 | |
| 5817 | ret = xmlOutputBufferClose(buf); |
| 5818 | return(ret); |
| 5819 | } |
| 5820 | |