Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tree.c : implemetation of access function for an XML tree. |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 6 | * Daniel.Veillard@w3.org |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 7 | * |
| 8 | * 14 Nov 2000 ht - Changed the name of function xmlBufferWriteChar under VMS |
| 9 | * as it was similar to xmlBufferWriteCHAR when compiling without case |
| 10 | * sensitivity. |
| 11 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 14 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 15 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 16 | #else |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 17 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 21 | #include <string.h> /* for memset() only ! */ |
| 22 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 23 | #ifdef HAVE_CTYPE_H |
| 24 | #include <ctype.h> |
| 25 | #endif |
| 26 | #ifdef HAVE_STDLIB_H |
| 27 | #include <stdlib.h> |
| 28 | #endif |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 29 | #ifdef HAVE_ZLIB_H |
| 30 | #include <zlib.h> |
| 31 | #endif |
| 32 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 33 | #include <libxml/xmlmemory.h> |
| 34 | #include <libxml/tree.h> |
| 35 | #include <libxml/parser.h> |
| 36 | #include <libxml/entities.h> |
| 37 | #include <libxml/valid.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 38 | #include <libxml/xmlerror.h> |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 39 | |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 40 | xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
| 41 | xmlChar xmlStringTextNoenc[] = |
| 42 | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
| 43 | xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 44 | int oldXMLWDcompatibility = 0; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 45 | int xmlIndentTreeOutput = 0; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 46 | xmlBufferAllocationScheme xmlBufferAllocScheme = XML_BUFFER_ALLOC_EXACT; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 47 | |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 48 | static int xmlCompressMode = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 49 | static int xmlCheckDTD = 1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 50 | int xmlSaveNoEmptyTags = 0; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 51 | |
| 52 | #define IS_BLANK(c) \ |
| 53 | (((c) == '\n') || ((c) == '\r') || ((c) == '\t') || ((c) == ' ')) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 54 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 55 | #define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 56 | xmlNodePtr ulccur = (n)->children; \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 57 | if (ulccur == NULL) { \ |
| 58 | (n)->last = NULL; \ |
| 59 | } else { \ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 60 | while (ulccur->next != NULL) { \ |
| 61 | ulccur->parent = (n); \ |
| 62 | ulccur = ulccur->next; \ |
| 63 | } \ |
| 64 | ulccur->parent = (n); \ |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 65 | (n)->last = ulccur; \ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 66 | }} |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 67 | |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 68 | /* #define DEBUG_BUFFER */ |
| 69 | /* #define DEBUG_TREE */ |
| 70 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 71 | /************************************************************************ |
| 72 | * * |
| 73 | * Allocation and deallocation of basic structures * |
| 74 | * * |
| 75 | ************************************************************************/ |
| 76 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 77 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 78 | * xmlSetBufferAllocationScheme: |
| 79 | * @scheme: allocation method to use |
| 80 | * |
| 81 | * Set the buffer allocation method. Types are |
| 82 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 83 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 84 | * improves performance |
| 85 | */ |
| 86 | void |
| 87 | xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { |
| 88 | xmlBufferAllocScheme = scheme; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * xmlGetBufferAllocationScheme: |
| 93 | * |
| 94 | * Types are |
| 95 | * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down |
| 96 | * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, |
| 97 | * improves performance |
| 98 | * |
| 99 | * Returns the current allocation scheme |
| 100 | */ |
| 101 | xmlBufferAllocationScheme |
| 102 | xmlGetBufferAllocationScheme() { |
| 103 | return xmlBufferAllocScheme; |
| 104 | } |
| 105 | |
| 106 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 107 | * xmlNewNs: |
| 108 | * @node: the element carrying the namespace |
| 109 | * @href: the URI associated |
| 110 | * @prefix: the prefix for the namespace |
| 111 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 112 | * Creation of a new Namespace. This function will refuse to create |
| 113 | * a namespace with a similar prefix than an existing one present on this |
| 114 | * node. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 115 | * We use href==NULL in the case of an element creation where the namespace |
| 116 | * was not defined. |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 117 | * Returns returns a new namespace pointer or NULL |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 118 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 119 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 120 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 121 | xmlNsPtr cur; |
| 122 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 123 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 124 | * Allocate a new Namespace and fill the fields. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 125 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 126 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 127 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 128 | xmlGenericError(xmlGenericErrorContext, |
| 129 | "xmlNewNs : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 130 | return(NULL); |
| 131 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 132 | memset(cur, 0, sizeof(xmlNs)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 133 | cur->type = XML_LOCAL_NAMESPACE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 134 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 135 | if (href != NULL) |
| 136 | cur->href = xmlStrdup(href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 137 | if (prefix != NULL) |
| 138 | cur->prefix = xmlStrdup(prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Add it at the end to preserve parsing order ... |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 142 | * and checks for existing use of the prefix |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 143 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 144 | if (node != NULL) { |
| 145 | if (node->nsDef == NULL) { |
| 146 | node->nsDef = cur; |
| 147 | } else { |
| 148 | xmlNsPtr prev = node->nsDef; |
| 149 | |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +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 | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 152 | xmlFreeNs(cur); |
| 153 | return(NULL); |
| 154 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 155 | while (prev->next != NULL) { |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 156 | prev = prev->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 157 | if (((prev->prefix == NULL) && (cur->prefix == NULL)) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 158 | (xmlStrEqual(prev->prefix, cur->prefix))) { |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 159 | xmlFreeNs(cur); |
| 160 | return(NULL); |
| 161 | } |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 162 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 163 | prev->next = cur; |
| 164 | } |
| 165 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 166 | return(cur); |
| 167 | } |
| 168 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 169 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 170 | * xmlSetNs: |
| 171 | * @node: a node in the document |
| 172 | * @ns: a namespace pointer |
| 173 | * |
| 174 | * Associate a namespace to a node, a posteriori. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 175 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 176 | void |
| 177 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 178 | if (node == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 179 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 180 | xmlGenericError(xmlGenericErrorContext, |
| 181 | "xmlSetNs: node == NULL\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 182 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | node->ns = ns; |
| 186 | } |
| 187 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 188 | /** |
| 189 | * xmlFreeNs: |
| 190 | * @cur: the namespace pointer |
| 191 | * |
| 192 | * Free up the structures associated to a namespace |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 193 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 194 | void |
| 195 | xmlFreeNs(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 196 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 197 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 198 | xmlGenericError(xmlGenericErrorContext, |
| 199 | "xmlFreeNs : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 200 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 203 | if (cur->href != NULL) xmlFree((char *) cur->href); |
| 204 | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 205 | memset(cur, -1, sizeof(xmlNs)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 206 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 209 | /** |
| 210 | * xmlFreeNsList: |
| 211 | * @cur: the first namespace pointer |
| 212 | * |
| 213 | * Free up all the structures associated to the chained namespaces. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 214 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 215 | void |
| 216 | xmlFreeNsList(xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 217 | xmlNsPtr next; |
| 218 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 219 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 220 | xmlGenericError(xmlGenericErrorContext, |
| 221 | "xmlFreeNsList : ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 222 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | while (cur != NULL) { |
| 226 | next = cur->next; |
| 227 | xmlFreeNs(cur); |
| 228 | cur = next; |
| 229 | } |
| 230 | } |
| 231 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 232 | /** |
| 233 | * xmlNewDtd: |
| 234 | * @doc: the document pointer |
| 235 | * @name: the DTD name |
| 236 | * @ExternalID: the external ID |
| 237 | * @SystemID: the system ID |
| 238 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 239 | * Creation of a new DTD for the external subset. To create an |
| 240 | * internal subset, use xmlCreateIntSubset(). |
| 241 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 242 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 243 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 244 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 245 | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
| 246 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 247 | xmlDtdPtr cur; |
| 248 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 249 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 250 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 251 | xmlGenericError(xmlGenericErrorContext, |
| 252 | "xmlNewDtd(%s): document %s already have a DTD %s\n", |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 253 | /* !!! */ (char *) name, doc->name, |
| 254 | /* !!! */ (char *)doc->extSubset->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 255 | #endif |
| 256 | return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Allocate a new DTD and fill the fields. |
| 261 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 262 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 263 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 264 | xmlGenericError(xmlGenericErrorContext, |
| 265 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 266 | return(NULL); |
| 267 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 268 | memset(cur, 0 , sizeof(xmlDtd)); |
| 269 | cur->type = XML_DTD_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 270 | |
| 271 | if (name != NULL) |
| 272 | cur->name = xmlStrdup(name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 273 | if (ExternalID != NULL) |
| 274 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 275 | if (SystemID != NULL) |
| 276 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 277 | if (doc != NULL) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 278 | doc->extSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 279 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 280 | |
| 281 | return(cur); |
| 282 | } |
| 283 | |
| 284 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 285 | * xmlGetIntSubset: |
| 286 | * @doc: the document pointer |
| 287 | * |
| 288 | * Get the internal subset of a document |
| 289 | * Returns a pointer to the DTD structure or NULL if not found |
| 290 | */ |
| 291 | |
| 292 | xmlDtdPtr |
| 293 | xmlGetIntSubset(xmlDocPtr doc) { |
| 294 | xmlNodePtr cur; |
| 295 | |
| 296 | if (doc == NULL) |
| 297 | return(NULL); |
| 298 | cur = doc->children; |
| 299 | while (cur != NULL) { |
| 300 | if (cur->type == XML_DTD_NODE) |
| 301 | return((xmlDtdPtr) cur); |
| 302 | cur = cur->next; |
| 303 | } |
| 304 | return((xmlDtdPtr) doc->intSubset); |
| 305 | } |
| 306 | |
| 307 | /** |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 308 | * xmlCreateIntSubset: |
| 309 | * @doc: the document pointer |
| 310 | * @name: the DTD name |
| 311 | * @ExternalID: the external ID |
| 312 | * @SystemID: the system ID |
| 313 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 314 | * Create the internal subset of a document |
| 315 | * Returns a pointer to the new DTD structure |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 316 | */ |
| 317 | xmlDtdPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 318 | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
| 319 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 320 | xmlDtdPtr cur; |
| 321 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 322 | if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 323 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 324 | xmlGenericError(xmlGenericErrorContext, |
| 325 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 326 | "xmlCreateIntSubset(): document %s already have an internal subset\n", |
| 327 | doc->name); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 328 | #endif |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 329 | return(NULL); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Allocate a new DTD and fill the fields. |
| 334 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 335 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 336 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 337 | xmlGenericError(xmlGenericErrorContext, |
| 338 | "xmlNewDtd : malloc failed\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 339 | return(NULL); |
| 340 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 341 | memset(cur, 0, sizeof(xmlDtd)); |
| 342 | cur->type = XML_DTD_NODE; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 343 | |
| 344 | if (name != NULL) |
| 345 | cur->name = xmlStrdup(name); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 346 | if (ExternalID != NULL) |
| 347 | cur->ExternalID = xmlStrdup(ExternalID); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 348 | if (SystemID != NULL) |
| 349 | cur->SystemID = xmlStrdup(SystemID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 350 | if (doc != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 351 | doc->intSubset = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 352 | cur->parent = doc; |
| 353 | cur->doc = doc; |
| 354 | if (doc->children == NULL) { |
| 355 | doc->children = (xmlNodePtr) cur; |
| 356 | doc->last = (xmlNodePtr) cur; |
| 357 | } else { |
| 358 | xmlNodePtr prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 359 | |
Daniel Veillard | b8f25c9 | 2000-08-19 19:52:36 +0000 | [diff] [blame] | 360 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 361 | prev = doc->children; |
| 362 | prev->prev = (xmlNodePtr) cur; |
| 363 | cur->next = prev; |
| 364 | doc->children = (xmlNodePtr) cur; |
| 365 | } else { |
| 366 | prev = doc->last; |
| 367 | prev->next = (xmlNodePtr) cur; |
| 368 | cur->prev = prev; |
| 369 | doc->last = (xmlNodePtr) cur; |
| 370 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 373 | return(cur); |
| 374 | } |
| 375 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 376 | /** |
| 377 | * xmlFreeDtd: |
| 378 | * @cur: the DTD structure to free up |
| 379 | * |
| 380 | * Free a DTD structure. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 381 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 382 | void |
| 383 | xmlFreeDtd(xmlDtdPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 384 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 385 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 386 | xmlGenericError(xmlGenericErrorContext, |
| 387 | "xmlFreeDtd : DTD == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 388 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 391 | if (cur->children != NULL) { |
| 392 | xmlNodePtr next, c = cur->children; |
| 393 | |
| 394 | /* |
| 395 | * Cleanup all the DTD comments they are not in the Dtd |
| 396 | * indexes. |
| 397 | */ |
| 398 | while (c != NULL) { |
| 399 | next = c->next; |
| 400 | if (c->type == XML_COMMENT_NODE) { |
| 401 | xmlUnlinkNode(c); |
| 402 | xmlFreeNode(c); |
| 403 | } |
| 404 | c = next; |
| 405 | } |
| 406 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 407 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 408 | if (cur->SystemID != NULL) xmlFree((char *) cur->SystemID); |
| 409 | if (cur->ExternalID != NULL) xmlFree((char *) cur->ExternalID); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 410 | /* TODO !!! */ |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 411 | if (cur->notations != NULL) |
| 412 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 413 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 414 | if (cur->elements != NULL) |
Daniel Veillard | 3b9def1 | 1999-01-31 22:15:06 +0000 | [diff] [blame] | 415 | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 416 | if (cur->attributes != NULL) |
| 417 | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 418 | if (cur->entities != NULL) |
| 419 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 420 | if (cur->pentities != NULL) |
| 421 | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 422 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 423 | memset(cur, -1, sizeof(xmlDtd)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 424 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 427 | /** |
| 428 | * xmlNewDoc: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 429 | * @version: xmlChar string giving the version of XML "1.0" |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 430 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 431 | * Returns a new document |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 432 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 433 | xmlDocPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 434 | xmlNewDoc(const xmlChar *version) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 435 | xmlDocPtr cur; |
| 436 | |
Daniel Veillard | 7eda845 | 2000-10-14 23:38:43 +0000 | [diff] [blame] | 437 | if (version == NULL) |
| 438 | version = (const xmlChar *) "1.0"; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * Allocate a new document and fill the fields. |
| 442 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 443 | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 444 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 445 | xmlGenericError(xmlGenericErrorContext, |
| 446 | "xmlNewDoc : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 447 | return(NULL); |
| 448 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 449 | memset(cur, 0, sizeof(xmlDoc)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 450 | cur->type = XML_DOCUMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 451 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 452 | cur->version = xmlStrdup(version); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 453 | cur->standalone = -1; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 454 | cur->compression = -1; /* not initialized */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 455 | cur->doc = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 456 | return(cur); |
| 457 | } |
| 458 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 459 | /** |
| 460 | * xmlFreeDoc: |
| 461 | * @cur: pointer to the document |
| 462 | * @: |
| 463 | * |
| 464 | * Free up all the structures used by a document, tree included. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 465 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 466 | void |
| 467 | xmlFreeDoc(xmlDocPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 468 | if (cur == NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 469 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 470 | xmlGenericError(xmlGenericErrorContext, |
| 471 | "xmlFreeDoc : document == NULL\n"); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 472 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 475 | if (cur->version != NULL) xmlFree((char *) cur->version); |
| 476 | if (cur->name != NULL) xmlFree((char *) cur->name); |
| 477 | if (cur->encoding != NULL) xmlFree((char *) cur->encoding); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 478 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 479 | if (cur->intSubset != NULL) xmlFreeDtd(cur->intSubset); |
| 480 | if (cur->extSubset != NULL) xmlFreeDtd(cur->extSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 481 | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 482 | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 483 | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 484 | if (cur->URL != NULL) xmlFree((char *) cur->URL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 485 | memset(cur, -1, sizeof(xmlDoc)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 486 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 489 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 490 | * xmlStringLenGetNodeList: |
| 491 | * @doc: the document |
| 492 | * @value: the value of the text |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 493 | * @len: the length of the string value |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 494 | * |
| 495 | * Parse the value string and build the node list associated. Should |
| 496 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 497 | * Returns a pointer to the first child |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 498 | */ |
| 499 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 500 | xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 501 | xmlNodePtr ret = NULL, last = NULL; |
| 502 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 503 | xmlChar *val; |
| 504 | const xmlChar *cur = value; |
| 505 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 506 | xmlEntityPtr ent; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 507 | |
| 508 | if (value == NULL) return(NULL); |
| 509 | |
| 510 | q = cur; |
| 511 | while ((*cur != 0) && (cur - value < len)) { |
| 512 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 513 | /* |
| 514 | * Save the current text. |
| 515 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 516 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 517 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 518 | xmlNodeAddContentLen(last, q, cur - q); |
| 519 | } else { |
| 520 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 521 | if (node == NULL) return(ret); |
| 522 | if (last == NULL) |
| 523 | last = ret = node; |
| 524 | else { |
| 525 | last->next = node; |
| 526 | node->prev = last; |
| 527 | last = node; |
| 528 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 531 | /* |
| 532 | * Read the entity string |
| 533 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 534 | cur++; |
| 535 | q = cur; |
| 536 | while ((*cur != 0) && (cur - value < len) && (*cur != ';')) cur++; |
| 537 | if ((*cur == 0) || (cur - value >= len)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 538 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 539 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 540 | "xmlStringLenGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 541 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 542 | return(ret); |
| 543 | } |
| 544 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 545 | /* |
| 546 | * Predefined entities don't generate nodes |
| 547 | */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 548 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 549 | ent = xmlGetDocEntity(doc, val); |
| 550 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 551 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 552 | if (last == NULL) { |
| 553 | node = xmlNewDocText(doc, ent->content); |
| 554 | last = ret = node; |
| 555 | } else |
| 556 | xmlNodeAddContent(last, ent->content); |
| 557 | |
| 558 | } else { |
| 559 | /* |
| 560 | * Create a new REFERENCE_REF node |
| 561 | */ |
| 562 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 563 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 564 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 565 | return(ret); |
| 566 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 567 | if (last == NULL) |
| 568 | last = ret = node; |
| 569 | else { |
| 570 | last->next = node; |
| 571 | node->prev = last; |
| 572 | last = node; |
| 573 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 574 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 575 | xmlFree(val); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 576 | } |
| 577 | cur++; |
| 578 | q = cur; |
| 579 | } else |
| 580 | cur++; |
| 581 | } |
| 582 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 583 | /* |
| 584 | * Handle the last piece of text. |
| 585 | */ |
| 586 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 587 | xmlNodeAddContentLen(last, q, cur - q); |
| 588 | } else { |
| 589 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 590 | if (node == NULL) return(ret); |
| 591 | if (last == NULL) |
| 592 | last = ret = node; |
| 593 | else { |
| 594 | last->next = node; |
| 595 | node->prev = last; |
| 596 | last = node; |
| 597 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | return(ret); |
| 601 | } |
| 602 | |
| 603 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 604 | * xmlStringGetNodeList: |
| 605 | * @doc: the document |
| 606 | * @value: the value of the attribute |
| 607 | * |
| 608 | * Parse the value string and build the node list associated. Should |
| 609 | * produce a flat tree with only TEXTs and ENTITY_REFs. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 610 | * Returns a pointer to the first child |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 611 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 612 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 613 | xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 614 | xmlNodePtr ret = NULL, last = NULL; |
| 615 | xmlNodePtr node; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 616 | xmlChar *val; |
| 617 | const xmlChar *cur = value; |
| 618 | const xmlChar *q; |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 619 | xmlEntityPtr ent; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 620 | |
| 621 | if (value == NULL) return(NULL); |
| 622 | |
| 623 | q = cur; |
| 624 | while (*cur != 0) { |
| 625 | if (*cur == '&') { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 626 | /* |
| 627 | * Save the current text. |
| 628 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 629 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 630 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 631 | xmlNodeAddContentLen(last, q, cur - q); |
| 632 | } else { |
| 633 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 634 | if (node == NULL) return(ret); |
| 635 | if (last == NULL) |
| 636 | last = ret = node; |
| 637 | else { |
| 638 | last->next = node; |
| 639 | node->prev = last; |
| 640 | last = node; |
| 641 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 644 | /* |
| 645 | * Read the entity string |
| 646 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 647 | cur++; |
| 648 | q = cur; |
| 649 | while ((*cur != 0) && (*cur != ';')) cur++; |
| 650 | if (*cur == 0) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 651 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 652 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 653 | "xmlStringGetNodeList: unterminated entity %30s\n", q); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 654 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 655 | return(ret); |
| 656 | } |
| 657 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 658 | /* |
| 659 | * Predefined entities don't generate nodes |
| 660 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 661 | val = xmlStrndup(q, cur - q); |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 662 | ent = xmlGetDocEntity(doc, val); |
| 663 | if ((ent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 664 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 665 | if (last == NULL) { |
| 666 | node = xmlNewDocText(doc, ent->content); |
| 667 | last = ret = node; |
| 668 | } else |
| 669 | xmlNodeAddContent(last, ent->content); |
| 670 | |
| 671 | } else { |
| 672 | /* |
| 673 | * Create a new REFERENCE_REF node |
| 674 | */ |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 675 | node = xmlNewReference(doc, val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 676 | if (node == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 677 | if (val != NULL) xmlFree(val); |
Daniel Veillard | 242590e | 1998-11-13 18:04:35 +0000 | [diff] [blame] | 678 | return(ret); |
| 679 | } |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 680 | if (last == NULL) |
| 681 | last = ret = node; |
| 682 | else { |
| 683 | last->next = node; |
| 684 | node->prev = last; |
| 685 | last = node; |
| 686 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 687 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 688 | xmlFree(val); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 689 | } |
| 690 | cur++; |
| 691 | q = cur; |
| 692 | } else |
| 693 | cur++; |
| 694 | } |
| 695 | if (cur != q) { |
Daniel Veillard | 25940b7 | 1998-10-29 05:51:30 +0000 | [diff] [blame] | 696 | /* |
| 697 | * Handle the last piece of text. |
| 698 | */ |
| 699 | if ((last != NULL) && (last->type == XML_TEXT_NODE)) { |
| 700 | xmlNodeAddContentLen(last, q, cur - q); |
| 701 | } else { |
| 702 | node = xmlNewDocTextLen(doc, q, cur - q); |
| 703 | if (node == NULL) return(ret); |
| 704 | if (last == NULL) |
| 705 | last = ret = node; |
| 706 | else { |
| 707 | last->next = node; |
| 708 | node->prev = last; |
| 709 | last = node; |
| 710 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | return(ret); |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * xmlNodeListGetString: |
| 718 | * @doc: the document |
| 719 | * @list: a Node list |
| 720 | * @inLine: should we replace entity contents or show their external form |
| 721 | * |
| 722 | * Returns the string equivalent to the text contained in the Node list |
| 723 | * made of TEXTs and ENTITY_REFs |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 724 | * Returns a pointer to the string copy, the calller must free it. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 725 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 726 | xmlChar * |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 727 | xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 728 | xmlNodePtr node = list; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 729 | xmlChar *ret = NULL; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 730 | xmlEntityPtr ent; |
| 731 | |
| 732 | if (list == NULL) return(NULL); |
| 733 | |
| 734 | while (node != NULL) { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 735 | if ((node->type == XML_TEXT_NODE) || |
| 736 | (node->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 737 | if (inLine) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 738 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 739 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 740 | #else |
| 741 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 742 | #endif |
| 743 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 744 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 745 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 746 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 747 | buffer = xmlEncodeEntitiesReentrant(doc, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 748 | #else |
| 749 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 750 | xmlBufferContent(node->content)); |
| 751 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 752 | if (buffer != NULL) { |
| 753 | ret = xmlStrcat(ret, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 754 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 757 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 758 | if (inLine) { |
| 759 | ent = xmlGetDocEntity(doc, node->name); |
| 760 | if (ent != NULL) |
| 761 | ret = xmlStrcat(ret, ent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 762 | else { |
| 763 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 764 | ret = xmlStrcat(ret, node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 765 | #else |
| 766 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 767 | #endif |
| 768 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 769 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 770 | xmlChar buf[2]; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 771 | buf[0] = '&'; buf[1] = 0; |
| 772 | ret = xmlStrncat(ret, buf, 1); |
| 773 | ret = xmlStrcat(ret, node->name); |
| 774 | buf[0] = ';'; buf[1] = 0; |
| 775 | ret = xmlStrncat(ret, buf, 1); |
| 776 | } |
| 777 | } |
| 778 | #if 0 |
| 779 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 780 | xmlGenericError(xmlGenericErrorContext, |
| 781 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 782 | node->type); |
| 783 | } |
| 784 | #endif |
| 785 | node = node->next; |
| 786 | } |
| 787 | return(ret); |
| 788 | } |
| 789 | |
| 790 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 791 | * xmlNodeListGetRawString: |
| 792 | * @doc: the document |
| 793 | * @list: a Node list |
| 794 | * @inLine: should we replace entity contents or show their external form |
| 795 | * |
| 796 | * Returns the string equivalent to the text contained in the Node list |
| 797 | * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString() |
| 798 | * this function doesn't do any character encoding handling. |
| 799 | * |
| 800 | * Returns a pointer to the string copy, the calller must free it. |
| 801 | */ |
| 802 | xmlChar * |
| 803 | xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine) { |
| 804 | xmlNodePtr node = list; |
| 805 | xmlChar *ret = NULL; |
| 806 | xmlEntityPtr ent; |
| 807 | |
| 808 | if (list == NULL) return(NULL); |
| 809 | |
| 810 | while (node != NULL) { |
| 811 | if (node->type == XML_TEXT_NODE) { |
| 812 | if (inLine) { |
| 813 | #ifndef XML_USE_BUFFER_CONTENT |
| 814 | ret = xmlStrcat(ret, node->content); |
| 815 | #else |
| 816 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 817 | #endif |
| 818 | } else { |
| 819 | xmlChar *buffer; |
| 820 | |
| 821 | #ifndef XML_USE_BUFFER_CONTENT |
| 822 | buffer = xmlEncodeSpecialChars(doc, node->content); |
| 823 | #else |
| 824 | buffer = xmlEncodeSpecialChars(doc, |
| 825 | xmlBufferContent(node->content)); |
| 826 | #endif |
| 827 | if (buffer != NULL) { |
| 828 | ret = xmlStrcat(ret, buffer); |
| 829 | xmlFree(buffer); |
| 830 | } |
| 831 | } |
| 832 | } else if (node->type == XML_ENTITY_REF_NODE) { |
| 833 | if (inLine) { |
| 834 | ent = xmlGetDocEntity(doc, node->name); |
| 835 | if (ent != NULL) |
| 836 | ret = xmlStrcat(ret, ent->content); |
| 837 | else { |
| 838 | #ifndef XML_USE_BUFFER_CONTENT |
| 839 | ret = xmlStrcat(ret, node->content); |
| 840 | #else |
| 841 | ret = xmlStrcat(ret, xmlBufferContent(node->content)); |
| 842 | #endif |
| 843 | } |
| 844 | } else { |
| 845 | xmlChar buf[2]; |
| 846 | buf[0] = '&'; buf[1] = 0; |
| 847 | ret = xmlStrncat(ret, buf, 1); |
| 848 | ret = xmlStrcat(ret, node->name); |
| 849 | buf[0] = ';'; buf[1] = 0; |
| 850 | ret = xmlStrncat(ret, buf, 1); |
| 851 | } |
| 852 | } |
| 853 | #if 0 |
| 854 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 855 | xmlGenericError(xmlGenericErrorContext, |
| 856 | "xmlGetNodeListString : invalide node type %d\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 857 | node->type); |
| 858 | } |
| 859 | #endif |
| 860 | node = node->next; |
| 861 | } |
| 862 | return(ret); |
| 863 | } |
| 864 | |
| 865 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 866 | * xmlNewProp: |
| 867 | * @node: the holding node |
| 868 | * @name: the name of the attribute |
| 869 | * @value: the value of the attribute |
| 870 | * |
| 871 | * Create a new property carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 872 | * Returns a pointer to the attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 873 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 874 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 875 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 876 | xmlAttrPtr cur; |
| 877 | |
| 878 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 879 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 880 | xmlGenericError(xmlGenericErrorContext, |
| 881 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 882 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 883 | return(NULL); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Allocate a new property and fill the fields. |
| 888 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 889 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 890 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 891 | xmlGenericError(xmlGenericErrorContext, |
| 892 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 893 | return(NULL); |
| 894 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 895 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 896 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 897 | |
| 898 | cur->parent = node; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 899 | cur->name = xmlStrdup(name); |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 900 | if (value != NULL) { |
| 901 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 902 | xmlNodePtr tmp; |
| 903 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 904 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 905 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 906 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 907 | tmp = cur->children; |
| 908 | while (tmp != NULL) { |
| 909 | tmp->parent = (xmlNodePtr) cur; |
| 910 | if (tmp->next == NULL) |
| 911 | cur->last = tmp; |
| 912 | tmp = tmp->next; |
| 913 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 914 | xmlFree(buffer); |
| 915 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 916 | |
| 917 | /* |
| 918 | * Add it at the end to preserve parsing order ... |
| 919 | */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 920 | if (node != NULL) { |
| 921 | if (node->properties == NULL) { |
| 922 | node->properties = cur; |
| 923 | } else { |
| 924 | xmlAttrPtr prev = node->properties; |
| 925 | |
| 926 | while (prev->next != NULL) prev = prev->next; |
| 927 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 928 | cur->prev = prev; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | return(cur); |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * xmlNewNsProp: |
| 936 | * @node: the holding node |
| 937 | * @ns: the namespace |
| 938 | * @name: the name of the attribute |
| 939 | * @value: the value of the attribute |
| 940 | * |
| 941 | * Create a new property tagged with a namespace and carried by a node. |
| 942 | * Returns a pointer to the attribute |
| 943 | */ |
| 944 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 945 | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 946 | const xmlChar *value) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 947 | xmlAttrPtr cur; |
| 948 | |
| 949 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 950 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 951 | xmlGenericError(xmlGenericErrorContext, |
| 952 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 953 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 954 | return(NULL); |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Allocate a new property and fill the fields. |
| 959 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 960 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 961 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 962 | xmlGenericError(xmlGenericErrorContext, |
| 963 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 964 | return(NULL); |
| 965 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 966 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 967 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 968 | |
| 969 | cur->parent = node; |
| 970 | if (node != NULL) |
| 971 | cur->doc = node->doc; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 972 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 973 | cur->name = xmlStrdup(name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 974 | if (value != NULL) { |
| 975 | xmlChar *buffer; |
| 976 | xmlNodePtr tmp; |
| 977 | |
| 978 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 979 | cur->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 980 | cur->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 981 | tmp = cur->children; |
| 982 | while (tmp != NULL) { |
| 983 | tmp->parent = (xmlNodePtr) cur; |
| 984 | if (tmp->next == NULL) |
| 985 | cur->last = tmp; |
| 986 | tmp = tmp->next; |
| 987 | } |
| 988 | xmlFree(buffer); |
| 989 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 990 | |
| 991 | /* |
| 992 | * Add it at the end to preserve parsing order ... |
| 993 | */ |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 994 | if (node != NULL) { |
| 995 | if (node->properties == NULL) { |
| 996 | node->properties = cur; |
| 997 | } else { |
| 998 | xmlAttrPtr prev = node->properties; |
| 999 | |
| 1000 | while (prev->next != NULL) prev = prev->next; |
| 1001 | prev->next = cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1002 | cur->prev = prev; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | return(cur); |
| 1006 | } |
| 1007 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1008 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1009 | * xmlNewDocProp: |
| 1010 | * @doc: the document |
| 1011 | * @name: the name of the attribute |
| 1012 | * @value: the value of the attribute |
| 1013 | * |
| 1014 | * Create a new property carried by a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1015 | * Returns a pointer to the attribute |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1016 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1017 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1018 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1019 | xmlAttrPtr cur; |
| 1020 | |
| 1021 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1022 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1023 | xmlGenericError(xmlGenericErrorContext, |
| 1024 | "xmlNewProp : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1025 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1026 | return(NULL); |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * Allocate a new property and fill the fields. |
| 1031 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1032 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1033 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1034 | xmlGenericError(xmlGenericErrorContext, |
| 1035 | "xmlNewProp : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1036 | return(NULL); |
| 1037 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1038 | memset(cur, 0, sizeof(xmlAttr)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1039 | cur->type = XML_ATTRIBUTE_NODE; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1040 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1041 | cur->name = xmlStrdup(name); |
| 1042 | cur->doc = doc; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1043 | if (value != NULL) { |
| 1044 | xmlNodePtr tmp; |
| 1045 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1046 | cur->children = xmlStringGetNodeList(doc, value); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1047 | cur->last = NULL; |
| 1048 | |
| 1049 | tmp = cur->children; |
| 1050 | while (tmp != NULL) { |
| 1051 | tmp->parent = (xmlNodePtr) cur; |
| 1052 | if (tmp->next == NULL) |
| 1053 | cur->last = tmp; |
| 1054 | tmp = tmp->next; |
| 1055 | } |
| 1056 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1057 | return(cur); |
| 1058 | } |
| 1059 | |
| 1060 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1061 | * xmlFreePropList: |
| 1062 | * @cur: the first property in the list |
| 1063 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1064 | * 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] | 1065 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1066 | void |
| 1067 | xmlFreePropList(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1068 | xmlAttrPtr next; |
| 1069 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1070 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1071 | xmlGenericError(xmlGenericErrorContext, |
| 1072 | "xmlFreePropList : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1073 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1074 | return; |
| 1075 | } |
| 1076 | while (cur != NULL) { |
| 1077 | next = cur->next; |
| 1078 | xmlFreeProp(cur); |
| 1079 | cur = next; |
| 1080 | } |
| 1081 | } |
| 1082 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1083 | /** |
| 1084 | * xmlFreeProp: |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1085 | * @cur: an attribute |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1086 | * |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1087 | * Free one attribute, all the content is freed too |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1088 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1089 | void |
| 1090 | xmlFreeProp(xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1091 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1092 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1093 | xmlGenericError(xmlGenericErrorContext, |
| 1094 | "xmlFreeProp : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1095 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1096 | return; |
| 1097 | } |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 1098 | /* Check for ID removal -> leading to invalid references ! */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1099 | if ((cur->parent != NULL) && |
| 1100 | (xmlIsID(cur->parent->doc, cur->parent, cur))) |
| 1101 | xmlRemoveID(cur->parent->doc, cur); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1102 | if (cur->name != NULL) xmlFree((char *) cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1103 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1104 | memset(cur, -1, sizeof(xmlAttr)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1105 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1108 | /** |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1109 | * xmlRemoveProp: |
| 1110 | * @cur: an attribute |
| 1111 | * |
| 1112 | * Unlink and free one attribute, all the content is freed too |
| 1113 | * Note this doesn't work for namespace definition attributes |
| 1114 | * |
| 1115 | * Returns 0 if success and -1 in case of error. |
| 1116 | */ |
| 1117 | int |
| 1118 | xmlRemoveProp(xmlAttrPtr cur) { |
| 1119 | xmlAttrPtr tmp; |
| 1120 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1121 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1122 | xmlGenericError(xmlGenericErrorContext, |
| 1123 | "xmlRemoveProp : cur == 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 | if (cur->parent == NULL) { |
| 1128 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1129 | xmlGenericError(xmlGenericErrorContext, |
| 1130 | "xmlRemoveProp : cur->parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1131 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1132 | return(-1); |
| 1133 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1134 | tmp = cur->parent->properties; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1135 | if (tmp == cur) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1136 | cur->parent->properties = cur->next; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1137 | xmlFreeProp(cur); |
| 1138 | return(0); |
| 1139 | } |
| 1140 | while (tmp != NULL) { |
| 1141 | if (tmp->next == cur) { |
| 1142 | tmp->next = cur->next; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1143 | if (tmp->next != NULL) |
| 1144 | tmp->next->prev = tmp; |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1145 | xmlFreeProp(cur); |
| 1146 | return(0); |
| 1147 | } |
| 1148 | tmp = tmp->next; |
| 1149 | } |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1150 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1151 | xmlGenericError(xmlGenericErrorContext, |
| 1152 | "xmlRemoveProp : attribute not owned by its node\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1153 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1154 | return(-1); |
| 1155 | } |
| 1156 | |
| 1157 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1158 | * xmlNewPI: |
| 1159 | * @name: the processing instruction name |
| 1160 | * @content: the PI content |
| 1161 | * |
| 1162 | * Creation of a processing instruction element. |
| 1163 | * Returns a pointer to the new node object. |
| 1164 | */ |
| 1165 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1166 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1167 | xmlNodePtr cur; |
| 1168 | |
| 1169 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1170 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1171 | xmlGenericError(xmlGenericErrorContext, |
| 1172 | "xmlNewPI : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1173 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1174 | return(NULL); |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Allocate a new node and fill the fields. |
| 1179 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1180 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1181 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1182 | xmlGenericError(xmlGenericErrorContext, |
| 1183 | "xmlNewPI : malloc failed\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1184 | return(NULL); |
| 1185 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1186 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1187 | cur->type = XML_PI_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1188 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1189 | cur->name = xmlStrdup(name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1190 | if (content != NULL) { |
| 1191 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1192 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1193 | #else |
| 1194 | cur->content = xmlBufferCreateSize(0); |
| 1195 | xmlBufferSetAllocationScheme(cur->content, |
| 1196 | xmlGetBufferAllocationScheme()); |
| 1197 | xmlBufferAdd(cur->content, content, -1); |
| 1198 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1199 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1200 | return(cur); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1204 | * xmlNewNode: |
| 1205 | * @ns: namespace if any |
| 1206 | * @name: the node name |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1207 | * |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 1208 | * Creation of a new node element. @ns is optionnal (NULL). |
| 1209 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1210 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1211 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1212 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1213 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1214 | xmlNodePtr cur; |
| 1215 | |
| 1216 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1217 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1218 | xmlGenericError(xmlGenericErrorContext, |
| 1219 | "xmlNewNode : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1220 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1221 | return(NULL); |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Allocate a new node and fill the fields. |
| 1226 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1227 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1228 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1229 | xmlGenericError(xmlGenericErrorContext, |
| 1230 | "xmlNewNode : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1231 | return(NULL); |
| 1232 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1233 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 3394284 | 1998-10-18 19:12:41 +0000 | [diff] [blame] | 1234 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1235 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1236 | cur->name = xmlStrdup(name); |
| 1237 | cur->ns = ns; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1238 | return(cur); |
| 1239 | } |
| 1240 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1241 | /** |
| 1242 | * xmlNewDocNode: |
| 1243 | * @doc: the document |
| 1244 | * @ns: namespace if any |
| 1245 | * @name: the node name |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1246 | * @content: the XML text content if any |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1247 | * |
| 1248 | * Creation of a new node element within a document. @ns and @content |
| 1249 | * are optionnal (NULL). |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1250 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1251 | * references, but XML special chars need to be escaped first by using |
| 1252 | * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't |
| 1253 | * need entities support. |
| 1254 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1255 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1256 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1257 | xmlNodePtr |
| 1258 | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1259 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1260 | xmlNodePtr cur; |
| 1261 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1262 | cur = xmlNewNode(ns, name); |
| 1263 | if (cur != NULL) { |
| 1264 | cur->doc = doc; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1265 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1266 | cur->children = xmlStringGetNodeList(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1267 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1268 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1269 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1270 | return(cur); |
| 1271 | } |
| 1272 | |
| 1273 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1274 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1275 | * xmlNewDocRawNode: |
| 1276 | * @doc: the document |
| 1277 | * @ns: namespace if any |
| 1278 | * @name: the node name |
| 1279 | * @content: the text content if any |
| 1280 | * |
| 1281 | * Creation of a new node element within a document. @ns and @content |
| 1282 | * are optionnal (NULL). |
| 1283 | * |
| 1284 | * Returns a pointer to the new node object. |
| 1285 | */ |
| 1286 | xmlNodePtr |
| 1287 | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
| 1288 | const xmlChar *name, const xmlChar *content) { |
| 1289 | xmlNodePtr cur; |
| 1290 | |
| 1291 | cur = xmlNewNode(ns, name); |
| 1292 | if (cur != NULL) { |
| 1293 | cur->doc = doc; |
| 1294 | if (content != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1295 | cur->children = xmlNewDocText(doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1296 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | return(cur); |
| 1300 | } |
| 1301 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1302 | /** |
| 1303 | * xmlNewDocFragment: |
| 1304 | * @doc: the document owning the fragment |
| 1305 | * |
| 1306 | * Creation of a new Fragment node. |
| 1307 | * Returns a pointer to the new node object. |
| 1308 | */ |
| 1309 | xmlNodePtr |
| 1310 | xmlNewDocFragment(xmlDocPtr doc) { |
| 1311 | xmlNodePtr cur; |
| 1312 | |
| 1313 | /* |
| 1314 | * Allocate a new DocumentFragment node and fill the fields. |
| 1315 | */ |
| 1316 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1317 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1318 | xmlGenericError(xmlGenericErrorContext, |
| 1319 | "xmlNewDocFragment : malloc failed\n"); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1320 | return(NULL); |
| 1321 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1322 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1323 | cur->type = XML_DOCUMENT_FRAG_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1324 | |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1325 | cur->doc = doc; |
Daniel Veillard | 2eac503 | 2000-01-09 21:08:56 +0000 | [diff] [blame] | 1326 | return(cur); |
| 1327 | } |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1328 | |
| 1329 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1330 | * xmlNewText: |
| 1331 | * @content: the text content |
| 1332 | * |
| 1333 | * Creation of a new text node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1334 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1335 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1336 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1337 | xmlNewText(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1338 | xmlNodePtr cur; |
| 1339 | |
| 1340 | /* |
| 1341 | * Allocate a new node and fill the fields. |
| 1342 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1343 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1344 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1345 | xmlGenericError(xmlGenericErrorContext, |
| 1346 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1347 | return(NULL); |
| 1348 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1349 | memset(cur, 0, sizeof(xmlNode)); |
| 1350 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1351 | |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 1352 | cur->name = xmlStringText; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1353 | if (content != NULL) { |
| 1354 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1355 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1356 | #else |
| 1357 | cur->content = xmlBufferCreateSize(0); |
| 1358 | xmlBufferSetAllocationScheme(cur->content, |
| 1359 | xmlGetBufferAllocationScheme()); |
| 1360 | xmlBufferAdd(cur->content, content, -1); |
| 1361 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1362 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1363 | return(cur); |
| 1364 | } |
| 1365 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1366 | /** |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1367 | * xmlNewTextChild: |
| 1368 | * @parent: the parent node |
| 1369 | * @ns: a namespace if any |
| 1370 | * @name: the name of the child |
| 1371 | * @content: the text content of the child if any. |
| 1372 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1373 | * 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] | 1374 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1375 | * a child TEXT node will be created containing the string content. |
| 1376 | * |
| 1377 | * Returns a pointer to the new node object. |
| 1378 | */ |
| 1379 | xmlNodePtr |
| 1380 | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
| 1381 | const xmlChar *name, const xmlChar *content) { |
| 1382 | xmlNodePtr cur, prev; |
| 1383 | |
| 1384 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1385 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1386 | xmlGenericError(xmlGenericErrorContext, |
| 1387 | "xmlNewTextChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1388 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1389 | return(NULL); |
| 1390 | } |
| 1391 | |
| 1392 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1393 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1394 | xmlGenericError(xmlGenericErrorContext, |
| 1395 | "xmlNewTextChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1396 | #endif |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1397 | return(NULL); |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Allocate a new node |
| 1402 | */ |
| 1403 | if (ns == NULL) |
| 1404 | cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content); |
| 1405 | else |
| 1406 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
| 1407 | if (cur == NULL) return(NULL); |
| 1408 | |
| 1409 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1410 | * add the new element at the end of the children list. |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1411 | */ |
| 1412 | cur->type = XML_ELEMENT_NODE; |
| 1413 | cur->parent = parent; |
| 1414 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1415 | if (parent->children == NULL) { |
| 1416 | parent->children = cur; |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1417 | parent->last = cur; |
| 1418 | } else { |
| 1419 | prev = parent->last; |
| 1420 | prev->next = cur; |
| 1421 | cur->prev = prev; |
| 1422 | parent->last = cur; |
| 1423 | } |
| 1424 | |
| 1425 | return(cur); |
| 1426 | } |
| 1427 | |
| 1428 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1429 | * xmlNewCharRef: |
| 1430 | * @doc: the document |
| 1431 | * @name: the char ref string, starting with # or "&# ... ;" |
| 1432 | * |
| 1433 | * Creation of a new character reference node. |
| 1434 | * Returns a pointer to the new node object. |
| 1435 | */ |
| 1436 | xmlNodePtr |
| 1437 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
| 1438 | xmlNodePtr cur; |
| 1439 | |
| 1440 | /* |
| 1441 | * Allocate a new node and fill the fields. |
| 1442 | */ |
| 1443 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
| 1444 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1445 | xmlGenericError(xmlGenericErrorContext, |
| 1446 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1447 | return(NULL); |
| 1448 | } |
| 1449 | memset(cur, 0, sizeof(xmlNode)); |
| 1450 | cur->type = XML_ENTITY_REF_NODE; |
| 1451 | |
| 1452 | cur->doc = doc; |
| 1453 | if (name[0] == '&') { |
| 1454 | int len; |
| 1455 | name++; |
| 1456 | len = xmlStrlen(name); |
| 1457 | if (name[len - 1] == ';') |
| 1458 | cur->name = xmlStrndup(name, len - 1); |
| 1459 | else |
| 1460 | cur->name = xmlStrndup(name, len); |
| 1461 | } else |
| 1462 | cur->name = xmlStrdup(name); |
| 1463 | return(cur); |
| 1464 | } |
| 1465 | |
| 1466 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1467 | * xmlNewReference: |
| 1468 | * @doc: the document |
| 1469 | * @name: the reference name, or the reference string with & and ; |
| 1470 | * |
| 1471 | * Creation of a new reference node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1472 | * Returns a pointer to the new node object. |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1473 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1474 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1475 | xmlNewReference(xmlDocPtr doc, const xmlChar *name) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1476 | xmlNodePtr cur; |
| 1477 | xmlEntityPtr ent; |
| 1478 | |
| 1479 | /* |
| 1480 | * Allocate a new node and fill the fields. |
| 1481 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1482 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1483 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1484 | xmlGenericError(xmlGenericErrorContext, |
| 1485 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1486 | return(NULL); |
| 1487 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1488 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1489 | cur->type = XML_ENTITY_REF_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1490 | |
Daniel Veillard | 10c6a8f | 1998-10-28 01:00:12 +0000 | [diff] [blame] | 1491 | cur->doc = doc; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1492 | if (name[0] == '&') { |
| 1493 | int len; |
| 1494 | name++; |
| 1495 | len = xmlStrlen(name); |
| 1496 | if (name[len - 1] == ';') |
| 1497 | cur->name = xmlStrndup(name, len - 1); |
| 1498 | else |
| 1499 | cur->name = xmlStrndup(name, len); |
| 1500 | } else |
| 1501 | cur->name = xmlStrdup(name); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1502 | |
| 1503 | ent = xmlGetDocEntity(doc, cur->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1504 | if (ent != NULL) { |
| 1505 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1506 | cur->content = ent->content; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1507 | #else |
| 1508 | /* |
| 1509 | * CJN 11.18.99 this might be a problem, since the xmlBuffer gets |
| 1510 | * a copy of this pointer. Let's hope we don't manipulate it |
| 1511 | * later |
| 1512 | */ |
| 1513 | cur->content = xmlBufferCreateSize(0); |
| 1514 | xmlBufferSetAllocationScheme(cur->content, |
| 1515 | xmlGetBufferAllocationScheme()); |
| 1516 | if (ent->content != NULL) |
| 1517 | xmlBufferAdd(cur->content, ent->content, -1); |
| 1518 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1519 | /* |
| 1520 | * The parent pointer in entity is a Dtd pointer and thus is NOT |
| 1521 | * updated. Not sure if this is 100% correct. |
| 1522 | * -George |
| 1523 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1524 | cur->children = (xmlNodePtr) ent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1525 | cur->last = (xmlNodePtr) ent; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1526 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1527 | return(cur); |
| 1528 | } |
| 1529 | |
| 1530 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1531 | * xmlNewDocText: |
| 1532 | * @doc: the document |
| 1533 | * @content: the text content |
| 1534 | * |
| 1535 | * Creation of a new text node within a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1536 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1537 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1538 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1539 | xmlNewDocText(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1540 | xmlNodePtr cur; |
| 1541 | |
| 1542 | cur = xmlNewText(content); |
| 1543 | if (cur != NULL) cur->doc = doc; |
| 1544 | return(cur); |
| 1545 | } |
| 1546 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1547 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1548 | * xmlNewTextLen: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1549 | * @content: the text content |
| 1550 | * @len: the text len. |
| 1551 | * |
| 1552 | * 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] | 1553 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1554 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1555 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1556 | xmlNewTextLen(const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1557 | xmlNodePtr cur; |
| 1558 | |
| 1559 | /* |
| 1560 | * Allocate a new node and fill the fields. |
| 1561 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1562 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1563 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1564 | xmlGenericError(xmlGenericErrorContext, |
| 1565 | "xmlNewText : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1566 | return(NULL); |
| 1567 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1568 | memset(cur, 0, sizeof(xmlNode)); |
| 1569 | cur->type = XML_TEXT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1570 | |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 1571 | cur->name = xmlStringText; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1572 | if (content != NULL) { |
| 1573 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1574 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1575 | #else |
| 1576 | cur->content = xmlBufferCreateSize(len); |
| 1577 | xmlBufferSetAllocationScheme(cur->content, |
| 1578 | xmlGetBufferAllocationScheme()); |
| 1579 | xmlBufferAdd(cur->content, content, len); |
| 1580 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1581 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1582 | return(cur); |
| 1583 | } |
| 1584 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1585 | /** |
| 1586 | * xmlNewDocTextLen: |
| 1587 | * @doc: the document |
| 1588 | * @content: the text content |
| 1589 | * @len: the text len. |
| 1590 | * |
| 1591 | * Creation of a new text node with an extra content lenght parameter. The |
| 1592 | * text node pertain to a given document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1593 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1594 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1595 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1596 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1597 | xmlNodePtr cur; |
| 1598 | |
| 1599 | cur = xmlNewTextLen(content, len); |
| 1600 | if (cur != NULL) cur->doc = doc; |
| 1601 | return(cur); |
| 1602 | } |
| 1603 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1604 | /** |
| 1605 | * xmlNewComment: |
| 1606 | * @content: the comment content |
| 1607 | * |
| 1608 | * Creation of a new node containing a comment. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1609 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1610 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1611 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1612 | xmlNewComment(const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1613 | xmlNodePtr cur; |
| 1614 | |
| 1615 | /* |
| 1616 | * Allocate a new node and fill the fields. |
| 1617 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1618 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1619 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1620 | xmlGenericError(xmlGenericErrorContext, |
| 1621 | "xmlNewComment : malloc failed\n"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1622 | return(NULL); |
| 1623 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1624 | memset(cur, 0, sizeof(xmlNode)); |
| 1625 | cur->type = XML_COMMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1626 | |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 1627 | cur->name = xmlStringComment; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1628 | if (content != NULL) { |
| 1629 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1630 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1631 | #else |
| 1632 | cur->content = xmlBufferCreateSize(0); |
| 1633 | xmlBufferSetAllocationScheme(cur->content, |
| 1634 | xmlGetBufferAllocationScheme()); |
| 1635 | xmlBufferAdd(cur->content, content, -1); |
| 1636 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1637 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1638 | return(cur); |
| 1639 | } |
| 1640 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1641 | /** |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1642 | * xmlNewCDataBlock: |
| 1643 | * @doc: the document |
| 1644 | * @content: the CData block content content |
| 1645 | * @len: the length of the block |
| 1646 | * |
| 1647 | * Creation of a new node containing a CData block. |
| 1648 | * Returns a pointer to the new node object. |
| 1649 | */ |
| 1650 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1651 | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1652 | xmlNodePtr cur; |
| 1653 | |
| 1654 | /* |
| 1655 | * Allocate a new node and fill the fields. |
| 1656 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1657 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1658 | if (cur == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1659 | xmlGenericError(xmlGenericErrorContext, |
| 1660 | "xmlNewCDataBlock : malloc failed\n"); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1661 | return(NULL); |
| 1662 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1663 | memset(cur, 0, sizeof(xmlNode)); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1664 | cur->type = XML_CDATA_SECTION_NODE; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1665 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1666 | if (content != NULL) { |
| 1667 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1668 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 1669 | #else |
| 1670 | cur->content = xmlBufferCreateSize(len); |
| 1671 | xmlBufferSetAllocationScheme(cur->content, |
| 1672 | xmlGetBufferAllocationScheme()); |
| 1673 | xmlBufferAdd(cur->content, content, len); |
| 1674 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1675 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1676 | return(cur); |
| 1677 | } |
| 1678 | |
| 1679 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1680 | * xmlNewDocComment: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1681 | * @doc: the document |
| 1682 | * @content: the comment content |
| 1683 | * |
| 1684 | * Creation of a new node containing a commentwithin a document. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1685 | * Returns a pointer to the new node object. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1686 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1687 | xmlNodePtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1688 | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1689 | xmlNodePtr cur; |
| 1690 | |
| 1691 | cur = xmlNewComment(content); |
| 1692 | if (cur != NULL) cur->doc = doc; |
| 1693 | return(cur); |
| 1694 | } |
| 1695 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1696 | /** |
| 1697 | * xmlSetTreeDoc: |
| 1698 | * @tree: the top element |
| 1699 | * @doc: the document |
| 1700 | * |
| 1701 | * update all nodes under the tree to point to the right document |
| 1702 | */ |
| 1703 | void |
| 1704 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
| 1705 | if (tree == NULL) |
| 1706 | return; |
| 1707 | if (tree->type == XML_ENTITY_DECL) |
| 1708 | return; |
| 1709 | if (tree->doc != doc) { |
| 1710 | if (tree->children != NULL) |
| 1711 | xmlSetListDoc(tree->children, doc); |
| 1712 | tree->doc = doc; |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * xmlSetListDoc: |
| 1718 | * @tree: the first element |
| 1719 | * @doc: the document |
| 1720 | * |
| 1721 | * update all nodes in the list to point to the right document |
| 1722 | */ |
| 1723 | void |
| 1724 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
| 1725 | xmlNodePtr cur; |
| 1726 | |
| 1727 | if (list == NULL) |
| 1728 | return; |
| 1729 | cur = list; |
| 1730 | while (cur != NULL) { |
| 1731 | if (cur->doc != doc) |
| 1732 | xmlSetTreeDoc(cur, doc); |
| 1733 | cur = cur->next; |
| 1734 | } |
| 1735 | } |
| 1736 | |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1737 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1738 | /** |
| 1739 | * xmlNewChild: |
| 1740 | * @parent: the parent node |
| 1741 | * @ns: a namespace if any |
| 1742 | * @name: the name of the child |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1743 | * @content: the XML content of the child if any. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1744 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1745 | * 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] | 1746 | * @ns and @content parameters are optionnal (NULL). If content is non NULL, |
| 1747 | * 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] | 1748 | * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities |
| 1749 | * references, but XML special chars need to be escaped first by using |
| 1750 | * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities |
| 1751 | * support is not needed. |
| 1752 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 1753 | * Returns a pointer to the new node object. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1754 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 1755 | xmlNodePtr |
| 1756 | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 1757 | const xmlChar *name, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1758 | xmlNodePtr cur, prev; |
| 1759 | |
| 1760 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1761 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1762 | xmlGenericError(xmlGenericErrorContext, |
| 1763 | "xmlNewChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1764 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1765 | return(NULL); |
| 1766 | } |
| 1767 | |
| 1768 | if (name == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1769 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1770 | xmlGenericError(xmlGenericErrorContext, |
| 1771 | "xmlNewChild : name == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1772 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1773 | return(NULL); |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Allocate a new node |
| 1778 | */ |
| 1779 | if (ns == NULL) |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1780 | cur = xmlNewDocNode(parent->doc, parent->ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1781 | else |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1782 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1783 | if (cur == NULL) return(NULL); |
| 1784 | |
| 1785 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1786 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1787 | */ |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 1788 | cur->type = XML_ELEMENT_NODE; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1789 | cur->parent = parent; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1790 | cur->doc = parent->doc; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1791 | if (parent->children == NULL) { |
| 1792 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1793 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1794 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1795 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1796 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 1797 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 1798 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | return(cur); |
| 1802 | } |
| 1803 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 1804 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1805 | * xmlAddNextSibling: |
| 1806 | * @cur: the child node |
| 1807 | * @elem: the new node |
| 1808 | * |
| 1809 | * Add a new element @elem as the next siblings of @cur |
| 1810 | * If the new element was already inserted in a document it is |
| 1811 | * first unlinked from its existing context. |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1812 | * As a result of text merging @elem may be freed. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1813 | * |
| 1814 | * Returns the new element or NULL in case of error. |
| 1815 | */ |
| 1816 | xmlNodePtr |
| 1817 | xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1818 | if (cur == 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 : cur == 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 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1826 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1827 | xmlGenericError(xmlGenericErrorContext, |
| 1828 | "xmlAddNextSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1829 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1830 | return(NULL); |
| 1831 | } |
| 1832 | |
| 1833 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1834 | |
| 1835 | if (elem->type == XML_TEXT_NODE) { |
| 1836 | if (cur->type == XML_TEXT_NODE) { |
| 1837 | #ifndef XML_USE_BUFFER_CONTENT |
| 1838 | xmlNodeAddContent(cur, elem->content); |
| 1839 | #else |
| 1840 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 1841 | #endif |
| 1842 | xmlFreeNode(elem); |
| 1843 | return(cur); |
| 1844 | } |
| 1845 | if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) { |
| 1846 | #ifndef XML_USE_BUFFER_CONTENT |
| 1847 | xmlChar *tmp; |
| 1848 | |
| 1849 | tmp = xmlStrdup(elem->content); |
| 1850 | tmp = xmlStrcat(tmp, cur->next->content); |
| 1851 | xmlNodeSetContent(cur->next, tmp); |
| 1852 | xmlFree(tmp); |
| 1853 | #else |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1854 | xmlBufferAddHead(cur->next->content, |
| 1855 | xmlBufferContent(elem->content), |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1856 | xmlBufferLength(elem->content)); |
| 1857 | #endif |
| 1858 | xmlFreeNode(elem); |
| 1859 | return(cur->next); |
| 1860 | } |
| 1861 | } |
| 1862 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1863 | if (elem->doc != cur->doc) { |
| 1864 | xmlSetTreeDoc(elem, cur->doc); |
| 1865 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1866 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1867 | elem->prev = cur; |
| 1868 | elem->next = cur->next; |
| 1869 | cur->next = elem; |
| 1870 | if (elem->next != NULL) |
| 1871 | elem->next->prev = elem; |
| 1872 | if ((elem->parent != NULL) && (elem->parent->last == cur)) |
| 1873 | elem->parent->last = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1874 | return(elem); |
| 1875 | } |
| 1876 | |
| 1877 | /** |
| 1878 | * xmlAddPrevSibling: |
| 1879 | * @cur: the child node |
| 1880 | * @elem: the new node |
| 1881 | * |
| 1882 | * Add a new element @elem as the previous siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1883 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1884 | * If the new element was already inserted in a document it is |
| 1885 | * first unlinked from its existing context. |
| 1886 | * |
| 1887 | * Returns the new element or NULL in case of error. |
| 1888 | */ |
| 1889 | xmlNodePtr |
| 1890 | xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1891 | if (cur == 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 : cur == 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 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1899 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1900 | xmlGenericError(xmlGenericErrorContext, |
| 1901 | "xmlAddPrevSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1902 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1903 | return(NULL); |
| 1904 | } |
| 1905 | |
| 1906 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1907 | |
| 1908 | if (elem->type == XML_TEXT_NODE) { |
| 1909 | if (cur->type == XML_TEXT_NODE) { |
| 1910 | #ifndef XML_USE_BUFFER_CONTENT |
| 1911 | xmlChar *tmp; |
| 1912 | |
| 1913 | tmp = xmlStrdup(elem->content); |
| 1914 | tmp = xmlStrcat(tmp, cur->content); |
| 1915 | xmlNodeSetContent(cur, tmp); |
| 1916 | xmlFree(tmp); |
| 1917 | #else |
| 1918 | xmlBufferAddHead(cur->content, xmlBufferContent(elem->content), |
| 1919 | xmlBufferLength(elem->content)); |
| 1920 | #endif |
| 1921 | xmlFreeNode(elem); |
| 1922 | return(cur); |
| 1923 | } |
| 1924 | if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) { |
| 1925 | #ifndef XML_USE_BUFFER_CONTENT |
| 1926 | xmlNodeAddContent(cur->prev, elem->content); |
| 1927 | #else |
| 1928 | xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content)); |
| 1929 | #endif |
| 1930 | xmlFreeNode(elem); |
| 1931 | return(cur->prev); |
| 1932 | } |
| 1933 | } |
| 1934 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 1935 | if (elem->doc != cur->doc) { |
| 1936 | xmlSetTreeDoc(elem, cur->doc); |
| 1937 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1938 | elem->parent = cur->parent; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1939 | elem->next = cur; |
| 1940 | elem->prev = cur->prev; |
| 1941 | cur->prev = elem; |
| 1942 | if (elem->prev != NULL) |
| 1943 | elem->prev->next = elem; |
| 1944 | if ((elem->parent != NULL) && (elem->parent->children == cur)) |
| 1945 | elem->parent->children = elem; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1946 | return(elem); |
| 1947 | } |
| 1948 | |
| 1949 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1950 | * xmlAddSibling: |
| 1951 | * @cur: the child node |
| 1952 | * @elem: the new node |
| 1953 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1954 | * Add a new element @elem to the list of siblings of @cur |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1955 | * merging adjacent TEXT nodes (@elem may be freed) |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1956 | * If the new element was already inserted in a document it is |
| 1957 | * first unlinked from its existing context. |
| 1958 | * |
| 1959 | * Returns the new element or NULL in case of error. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1960 | */ |
| 1961 | xmlNodePtr |
| 1962 | xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { |
| 1963 | xmlNodePtr parent; |
| 1964 | |
| 1965 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1966 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1967 | xmlGenericError(xmlGenericErrorContext, |
| 1968 | "xmlAddSibling : cur == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1969 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1970 | return(NULL); |
| 1971 | } |
| 1972 | |
| 1973 | if (elem == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1974 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1975 | xmlGenericError(xmlGenericErrorContext, |
| 1976 | "xmlAddSibling : elem == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 1977 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1978 | return(NULL); |
| 1979 | } |
| 1980 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1981 | /* |
| 1982 | * Constant time is we can rely on the ->parent->last to find |
| 1983 | * the last sibling. |
| 1984 | */ |
| 1985 | if ((cur->parent != NULL) && |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1986 | (cur->parent->children != NULL) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1987 | (cur->parent->last != NULL) && |
| 1988 | (cur->parent->last->next == NULL)) { |
| 1989 | cur = cur->parent->last; |
| 1990 | } else { |
| 1991 | while (cur->next != NULL) cur = cur->next; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 1994 | xmlUnlinkNode(elem); |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 1995 | |
| 1996 | if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { |
| 1997 | #ifndef XML_USE_BUFFER_CONTENT |
| 1998 | xmlNodeAddContent(cur, elem->content); |
| 1999 | #else |
| 2000 | xmlNodeAddContent(cur, xmlBufferContent(elem->content)); |
| 2001 | #endif |
| 2002 | xmlFreeNode(elem); |
| 2003 | return(cur); |
| 2004 | } |
| 2005 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2006 | if (elem->doc != cur->doc) { |
| 2007 | xmlSetTreeDoc(elem, cur->doc); |
| 2008 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2009 | parent = cur->parent; |
| 2010 | elem->prev = cur; |
| 2011 | elem->next = NULL; |
| 2012 | elem->parent = parent; |
| 2013 | cur->next = elem; |
| 2014 | if (parent != NULL) |
| 2015 | parent->last = elem; |
| 2016 | |
| 2017 | return(elem); |
| 2018 | } |
| 2019 | |
| 2020 | /** |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2021 | * xmlAddChildList: |
| 2022 | * @parent: the parent node |
| 2023 | * @cur: the first node in the list |
| 2024 | * |
| 2025 | * 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] | 2026 | * merging adjacent TEXT nodes (@cur may be freed) |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2027 | * |
| 2028 | * Returns the last child or NULL in case of error. |
| 2029 | */ |
| 2030 | xmlNodePtr |
| 2031 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
| 2032 | xmlNodePtr prev; |
| 2033 | |
| 2034 | if (parent == NULL) { |
| 2035 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2036 | xmlGenericError(xmlGenericErrorContext, |
| 2037 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2038 | #endif |
| 2039 | return(NULL); |
| 2040 | } |
| 2041 | |
| 2042 | if (cur == NULL) { |
| 2043 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2044 | xmlGenericError(xmlGenericErrorContext, |
| 2045 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2046 | #endif |
| 2047 | return(NULL); |
| 2048 | } |
| 2049 | |
| 2050 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2051 | (cur->doc != parent->doc)) { |
| 2052 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2053 | xmlGenericError(xmlGenericErrorContext, |
| 2054 | "Elements moved to a different document\n"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2055 | #endif |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * add the first element at the end of the children list. |
| 2060 | */ |
| 2061 | if (parent->children == NULL) { |
| 2062 | parent->children = cur; |
| 2063 | } else { |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2064 | /* |
| 2065 | * If cur and parent->last both are TEXT nodes, then merge them. |
| 2066 | */ |
| 2067 | if ((cur->type == XML_TEXT_NODE) && |
| 2068 | (parent->last->type == XML_TEXT_NODE)) { |
| 2069 | #ifndef XML_USE_BUFFER_CONTENT |
| 2070 | xmlNodeAddContent(parent->last, cur->content); |
| 2071 | #else |
| 2072 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2073 | #endif |
| 2074 | /* |
| 2075 | * if it's the only child, nothing more to be done. |
| 2076 | */ |
| 2077 | if (cur->next == NULL) { |
| 2078 | xmlFreeNode(cur); |
| 2079 | return(parent->last); |
| 2080 | } |
| 2081 | prev = cur; |
| 2082 | cur = cur->next; |
| 2083 | xmlFreeNode(prev); |
| 2084 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2085 | prev = parent->last; |
| 2086 | prev->next = cur; |
| 2087 | cur->prev = prev; |
| 2088 | } |
| 2089 | while (cur->next != NULL) { |
| 2090 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2091 | if (cur->doc != parent->doc) { |
| 2092 | xmlSetTreeDoc(cur, parent->doc); |
| 2093 | } |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2094 | cur = cur->next; |
| 2095 | } |
| 2096 | cur->parent = parent; |
| 2097 | cur->doc = parent->doc; /* the parent may not be linked to a doc ! */ |
| 2098 | parent->last = cur; |
| 2099 | |
| 2100 | return(cur); |
| 2101 | } |
| 2102 | |
| 2103 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2104 | * xmlAddChild: |
| 2105 | * @parent: the parent node |
| 2106 | * @cur: the child node |
| 2107 | * |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2108 | * Add a new child element, to @parent, at the end of the child list |
| 2109 | * merging adjacent TEXT nodes (in which case @cur is freed) |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2110 | * Returns the child or NULL in case of error. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2111 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2112 | xmlNodePtr |
| 2113 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2114 | xmlNodePtr prev; |
| 2115 | |
| 2116 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2117 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2118 | xmlGenericError(xmlGenericErrorContext, |
| 2119 | "xmlAddChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2120 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2121 | return(NULL); |
| 2122 | } |
| 2123 | |
| 2124 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2125 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2126 | xmlGenericError(xmlGenericErrorContext, |
| 2127 | "xmlAddChild : child == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2128 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2129 | return(NULL); |
| 2130 | } |
| 2131 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2132 | if ((cur->doc != NULL) && (parent->doc != NULL) && |
| 2133 | (cur->doc != parent->doc)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2134 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2135 | xmlGenericError(xmlGenericErrorContext, |
| 2136 | "Elements moved to a different document\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2137 | #endif |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2140 | /* |
Daniel Veillard | 683cb02 | 2000-10-22 12:04:13 +0000 | [diff] [blame] | 2141 | * If cur is a TEXT node, merge its content with adjacent TEXT nodes |
| 2142 | * or with parent->content if parent->content != NULL. |
| 2143 | * cur is then freed. |
| 2144 | */ |
| 2145 | if (cur->type == XML_TEXT_NODE) { |
| 2146 | if (((parent->type == XML_ELEMENT_NODE) || |
| 2147 | (parent->type == XML_TEXT_NODE)) && |
| 2148 | (parent->content != NULL)) { |
| 2149 | #ifndef XML_USE_BUFFER_CONTENT |
| 2150 | xmlNodeAddContent(parent, cur->content); |
| 2151 | #else |
| 2152 | xmlNodeAddContent(parent, xmlBufferContent(cur->content)); |
| 2153 | #endif |
| 2154 | xmlFreeNode(cur); |
| 2155 | return(parent); |
| 2156 | } |
| 2157 | if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE)) { |
| 2158 | #ifndef XML_USE_BUFFER_CONTENT |
| 2159 | xmlNodeAddContent(parent->last, cur->content); |
| 2160 | #else |
| 2161 | xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); |
| 2162 | #endif |
| 2163 | xmlFreeNode(cur); |
| 2164 | return(parent->last); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | /* |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2169 | * add the new element at the end of the children list. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2170 | */ |
| 2171 | cur->parent = parent; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2172 | if (cur->doc != parent->doc) { |
| 2173 | xmlSetTreeDoc(cur, parent->doc); |
| 2174 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2175 | |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2176 | /* |
| 2177 | * Handle the case where parent->content != NULL, in that case it will |
| 2178 | * create a intermediate TEXT node. |
| 2179 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2180 | if (((parent->type == XML_ELEMENT_NODE) || (parent->type == XML_TEXT_NODE)) && |
| 2181 | (parent->content != NULL)) { |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2182 | xmlNodePtr text; |
| 2183 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2184 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2185 | text = xmlNewDocText(parent->doc, parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2186 | #else |
| 2187 | text = xmlNewDocText(parent->doc, xmlBufferContent(parent->content)); |
| 2188 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2189 | if (text != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2190 | text->next = parent->children; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2191 | if (text->next != NULL) |
| 2192 | text->next->prev = text; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2193 | parent->children = text; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2194 | UPDATE_LAST_CHILD_AND_PARENT(parent) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2195 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2196 | xmlFree(parent->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2197 | #else |
| 2198 | xmlBufferFree(parent->content); |
| 2199 | #endif |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2200 | parent->content = NULL; |
| 2201 | } |
| 2202 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2203 | if (parent->children == NULL) { |
| 2204 | parent->children = cur; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2205 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2206 | } else { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2207 | prev = parent->last; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2208 | prev->next = cur; |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 2209 | cur->prev = prev; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2210 | parent->last = cur; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | return(cur); |
| 2214 | } |
| 2215 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2216 | /** |
| 2217 | * xmlGetLastChild: |
| 2218 | * @parent: the parent node |
| 2219 | * |
| 2220 | * Search the last child of a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2221 | * Returns the last child or NULL if none. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2222 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2223 | xmlNodePtr |
| 2224 | xmlGetLastChild(xmlNodePtr parent) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2225 | if (parent == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2226 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2227 | xmlGenericError(xmlGenericErrorContext, |
| 2228 | "xmlGetLastChild : parent == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2229 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2230 | return(NULL); |
| 2231 | } |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2232 | return(parent->last); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2235 | /** |
| 2236 | * xmlFreeNodeList: |
| 2237 | * @cur: the first node in the list |
| 2238 | * |
| 2239 | * 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] | 2240 | * the children are freed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2241 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2242 | void |
| 2243 | xmlFreeNodeList(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2244 | xmlNodePtr next; |
| 2245 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2246 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2247 | xmlGenericError(xmlGenericErrorContext, |
| 2248 | "xmlFreeNodeList : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2249 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2250 | return; |
| 2251 | } |
| 2252 | while (cur != NULL) { |
| 2253 | next = cur->next; |
| 2254 | xmlFreeNode(cur); |
| 2255 | cur = next; |
| 2256 | } |
| 2257 | } |
| 2258 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2259 | /** |
| 2260 | * xmlFreeNode: |
| 2261 | * @cur: the node |
| 2262 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2263 | * Free a node, this is a recursive behaviour, all the children are freed too. |
| 2264 | * This doesn't unlink the child from the list, use xmlUnlinkNode() first. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2265 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 2266 | void |
| 2267 | xmlFreeNode(xmlNodePtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2268 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2269 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2270 | xmlGenericError(xmlGenericErrorContext, |
| 2271 | "xmlFreeNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2272 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2275 | if (cur->type == XML_DTD_NODE) |
| 2276 | return; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2277 | cur->doc = NULL; |
| 2278 | cur->parent = NULL; |
| 2279 | cur->next = NULL; |
| 2280 | cur->prev = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2281 | if ((cur->children != NULL) && |
| 2282 | (cur->type != XML_ENTITY_REF_NODE)) |
| 2283 | xmlFreeNodeList(cur->children); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 2284 | if (cur->properties != NULL) xmlFreePropList(cur->properties); |
| 2285 | if (cur->type != XML_ENTITY_REF_NODE) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2286 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2287 | if (cur->content != NULL) xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2288 | #else |
| 2289 | if (cur->content != NULL) xmlBufferFree(cur->content); |
| 2290 | #endif |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 2291 | if ((cur->name != NULL) && |
| 2292 | (cur->name != xmlStringText) && |
| 2293 | (cur->name != xmlStringTextNoenc) && |
| 2294 | (cur->name != xmlStringComment)) |
| 2295 | xmlFree((char *) cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2296 | if (cur->nsDef != NULL) xmlFreeNsList(cur->nsDef); |
| 2297 | memset(cur, -1, sizeof(xmlNode)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2298 | xmlFree(cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2301 | /** |
| 2302 | * xmlUnlinkNode: |
| 2303 | * @cur: the node |
| 2304 | * |
| 2305 | * Unlink a node from it's current context, the node is not freed |
| 2306 | */ |
| 2307 | void |
| 2308 | xmlUnlinkNode(xmlNodePtr cur) { |
| 2309 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2310 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2311 | xmlGenericError(xmlGenericErrorContext, |
| 2312 | "xmlUnlinkNode : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2313 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2314 | return; |
| 2315 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2316 | if ((cur->parent != NULL) && (cur->parent->children == cur)) |
| 2317 | cur->parent->children = cur->next; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2318 | if ((cur->parent != NULL) && (cur->parent->last == cur)) |
| 2319 | cur->parent->last = cur->prev; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 2320 | if (cur->next != NULL) |
| 2321 | cur->next->prev = cur->prev; |
| 2322 | if (cur->prev != NULL) |
| 2323 | cur->prev->next = cur->next; |
| 2324 | cur->next = cur->prev = NULL; |
| 2325 | cur->parent = NULL; |
| 2326 | } |
| 2327 | |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2328 | /** |
| 2329 | * xmlReplaceNode: |
| 2330 | * @old: the old node |
| 2331 | * @cur: the node |
| 2332 | * |
| 2333 | * Unlink the old node from it's current context, prune the new one |
| 2334 | * at the same place. If cur was already inserted in a document it is |
| 2335 | * first unlinked from its existing context. |
| 2336 | * |
| 2337 | * Returns the old node |
| 2338 | */ |
| 2339 | xmlNodePtr |
| 2340 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
| 2341 | if (old == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2342 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2343 | xmlGenericError(xmlGenericErrorContext, |
| 2344 | "xmlReplaceNode : old == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2345 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2346 | return(NULL); |
| 2347 | } |
| 2348 | if (cur == NULL) { |
| 2349 | xmlUnlinkNode(old); |
| 2350 | return(old); |
| 2351 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2352 | if (cur == old) { |
| 2353 | return(old); |
| 2354 | } |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2355 | xmlUnlinkNode(cur); |
| 2356 | cur->doc = old->doc; |
| 2357 | cur->parent = old->parent; |
| 2358 | cur->next = old->next; |
| 2359 | if (cur->next != NULL) |
| 2360 | cur->next->prev = cur; |
| 2361 | cur->prev = old->prev; |
| 2362 | if (cur->prev != NULL) |
| 2363 | cur->prev->next = cur; |
| 2364 | if (cur->parent != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2365 | if (cur->parent->children == old) |
| 2366 | cur->parent->children = cur; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2367 | if (cur->parent->last == old) |
| 2368 | cur->parent->last = cur; |
| 2369 | } |
| 2370 | old->next = old->prev = NULL; |
| 2371 | old->parent = NULL; |
| 2372 | return(old); |
| 2373 | } |
| 2374 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2375 | /************************************************************************ |
| 2376 | * * |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2377 | * Copy operations * |
| 2378 | * * |
| 2379 | ************************************************************************/ |
| 2380 | |
| 2381 | /** |
| 2382 | * xmlCopyNamespace: |
| 2383 | * @cur: the namespace |
| 2384 | * |
| 2385 | * Do a copy of the namespace. |
| 2386 | * |
| 2387 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2388 | */ |
| 2389 | xmlNsPtr |
| 2390 | xmlCopyNamespace(xmlNsPtr cur) { |
| 2391 | xmlNsPtr ret; |
| 2392 | |
| 2393 | if (cur == NULL) return(NULL); |
| 2394 | switch (cur->type) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2395 | case XML_LOCAL_NAMESPACE: |
| 2396 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
| 2397 | break; |
| 2398 | default: |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2399 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2400 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2401 | "xmlCopyNamespace: invalid type %d\n", cur->type); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 2402 | #endif |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2403 | return(NULL); |
| 2404 | } |
| 2405 | return(ret); |
| 2406 | } |
| 2407 | |
| 2408 | /** |
| 2409 | * xmlCopyNamespaceList: |
| 2410 | * @cur: the first namespace |
| 2411 | * |
| 2412 | * Do a copy of an namespace list. |
| 2413 | * |
| 2414 | * Returns: a new xmlNsPtr, or NULL in case of error. |
| 2415 | */ |
| 2416 | xmlNsPtr |
| 2417 | xmlCopyNamespaceList(xmlNsPtr cur) { |
| 2418 | xmlNsPtr ret = NULL; |
| 2419 | xmlNsPtr p = NULL,q; |
| 2420 | |
| 2421 | while (cur != NULL) { |
| 2422 | q = xmlCopyNamespace(cur); |
| 2423 | if (p == NULL) { |
| 2424 | ret = p = q; |
| 2425 | } else { |
| 2426 | p->next = q; |
| 2427 | p = q; |
| 2428 | } |
| 2429 | cur = cur->next; |
| 2430 | } |
| 2431 | return(ret); |
| 2432 | } |
| 2433 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2434 | static xmlNodePtr |
| 2435 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2436 | /** |
| 2437 | * xmlCopyProp: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2438 | * @target: the element where the attribute will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2439 | * @cur: the attribute |
| 2440 | * |
| 2441 | * Do a copy of the attribute. |
| 2442 | * |
| 2443 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2444 | */ |
| 2445 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2446 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2447 | xmlAttrPtr ret; |
| 2448 | |
| 2449 | if (cur == NULL) return(NULL); |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2450 | if (target != NULL) |
| 2451 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
| 2452 | else if (cur->parent != NULL) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2453 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
| 2454 | else if (cur->children != NULL) |
| 2455 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2456 | else |
| 2457 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
| 2458 | if (ret == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2459 | ret->parent = target; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2460 | |
| 2461 | if ((cur->ns != NULL) && (target != NULL)) { |
| 2462 | xmlNsPtr ns; |
| 2463 | |
| 2464 | ns = xmlSearchNs(target->doc, target, cur->ns->prefix); |
| 2465 | ret->ns = ns; |
| 2466 | } else |
| 2467 | ret->ns = NULL; |
| 2468 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2469 | if (cur->children != NULL) { |
| 2470 | xmlNodePtr tmp; |
| 2471 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2472 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2473 | ret->last = NULL; |
| 2474 | tmp = ret->children; |
| 2475 | while (tmp != NULL) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2476 | /* tmp->parent = (xmlNodePtr)ret; */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2477 | if (tmp->next == NULL) |
| 2478 | ret->last = tmp; |
| 2479 | tmp = tmp->next; |
| 2480 | } |
| 2481 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2482 | return(ret); |
| 2483 | } |
| 2484 | |
| 2485 | /** |
| 2486 | * xmlCopyPropList: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2487 | * @target: the element where the attributes will be grafted |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2488 | * @cur: the first attribute |
| 2489 | * |
| 2490 | * Do a copy of an attribute list. |
| 2491 | * |
| 2492 | * Returns: a new xmlAttrPtr, or NULL in case of error. |
| 2493 | */ |
| 2494 | xmlAttrPtr |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2495 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2496 | xmlAttrPtr ret = NULL; |
| 2497 | xmlAttrPtr p = NULL,q; |
| 2498 | |
| 2499 | while (cur != NULL) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2500 | q = xmlCopyProp(target, cur); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2501 | if (p == NULL) { |
| 2502 | ret = p = q; |
| 2503 | } else { |
| 2504 | p->next = q; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2505 | q->prev = p; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2506 | p = q; |
| 2507 | } |
| 2508 | cur = cur->next; |
| 2509 | } |
| 2510 | return(ret); |
| 2511 | } |
| 2512 | |
| 2513 | /* |
Daniel Veillard | 11a48ec | 1999-11-23 10:40:46 +0000 | [diff] [blame] | 2514 | * NOTE abeut the CopyNode operations ! |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2515 | * |
| 2516 | * They are splitted into external and internal parts for one |
| 2517 | * tricky reason: namespaces. Doing a direct copy of a node |
| 2518 | * say RPM:Copyright without changing the namespace pointer to |
| 2519 | * something else can produce stale links. One way to do it is |
| 2520 | * to keep a reference counter but this doesn't work as soon |
| 2521 | * as one move the element or the subtree out of the scope of |
| 2522 | * the existing namespace. The actual solution seems to add |
| 2523 | * a copy of the namespace at the top of the copied tree if |
| 2524 | * not available in the subtree. |
| 2525 | * Hence two functions, the public front-end call the inner ones |
| 2526 | */ |
| 2527 | |
| 2528 | static xmlNodePtr |
| 2529 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent); |
| 2530 | |
| 2531 | static xmlNodePtr |
| 2532 | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
| 2533 | int recursive) { |
| 2534 | xmlNodePtr ret; |
| 2535 | |
| 2536 | if (node == NULL) return(NULL); |
| 2537 | /* |
| 2538 | * Allocate a new node and fill the fields. |
| 2539 | */ |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2540 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2541 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2542 | xmlGenericError(xmlGenericErrorContext, |
| 2543 | "xmlStaticCopyNode : malloc failed\n"); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2544 | return(NULL); |
| 2545 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2546 | memset(ret, 0, sizeof(xmlNode)); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2547 | ret->type = node->type; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2548 | |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2549 | ret->doc = doc; |
| 2550 | ret->parent = parent; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2551 | if (node->name != NULL) |
| 2552 | ret->name = xmlStrdup(node->name); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2553 | if ((node->content != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
| 2554 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2555 | ret->content = xmlStrdup(node->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 2556 | #else |
| 2557 | ret->content = xmlBufferCreateSize(xmlBufferLength(node->content)); |
| 2558 | xmlBufferSetAllocationScheme(ret->content, |
| 2559 | xmlGetBufferAllocationScheme()); |
| 2560 | xmlBufferAdd(ret->content, |
| 2561 | xmlBufferContent(node->content), |
| 2562 | xmlBufferLength(node->content)); |
| 2563 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2564 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2565 | if (parent != NULL) |
| 2566 | xmlAddChild(parent, ret); |
| 2567 | |
| 2568 | if (!recursive) return(ret); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2569 | if (node->nsDef != NULL) |
| 2570 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
| 2571 | |
| 2572 | if (node->ns != NULL) { |
| 2573 | xmlNsPtr ns; |
| 2574 | |
| 2575 | ns = xmlSearchNs(doc, ret, node->ns->prefix); |
| 2576 | if (ns == NULL) { |
| 2577 | /* |
| 2578 | * Humm, we are copying an element whose namespace is defined |
| 2579 | * out of the new tree scope. Search it in the original tree |
| 2580 | * and add it at the top of the new tree |
| 2581 | */ |
| 2582 | ns = xmlSearchNs(node->doc, node, node->ns->prefix); |
| 2583 | if (ns != NULL) { |
| 2584 | xmlNodePtr root = ret; |
| 2585 | |
| 2586 | while (root->parent != NULL) root = root->parent; |
| 2587 | xmlNewNs(root, ns->href, ns->prefix); |
| 2588 | } |
| 2589 | } else { |
| 2590 | /* |
| 2591 | * reference the existing namespace definition in our own tree. |
| 2592 | */ |
| 2593 | ret->ns = ns; |
| 2594 | } |
| 2595 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2596 | if (node->properties != NULL) |
| 2597 | ret->properties = xmlCopyPropList(ret, node->properties); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2598 | if (node->children != NULL) |
| 2599 | ret->children = xmlStaticCopyNodeList(node->children, doc, ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2600 | UPDATE_LAST_CHILD_AND_PARENT(ret) |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2601 | return(ret); |
| 2602 | } |
| 2603 | |
| 2604 | static xmlNodePtr |
| 2605 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
| 2606 | xmlNodePtr ret = NULL; |
| 2607 | xmlNodePtr p = NULL,q; |
| 2608 | |
| 2609 | while (node != NULL) { |
| 2610 | q = xmlStaticCopyNode(node, doc, parent, 1); |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2611 | if (ret == NULL) { |
| 2612 | q->prev = NULL; |
| 2613 | ret = p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2614 | } else { |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2615 | p->next = q; |
| 2616 | q->prev = p; |
| 2617 | p = q; |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2618 | } |
| 2619 | node = node->next; |
| 2620 | } |
| 2621 | return(ret); |
| 2622 | } |
| 2623 | |
| 2624 | /** |
| 2625 | * xmlCopyNode: |
| 2626 | * @node: the node |
| 2627 | * @recursive: if 1 do a recursive copy. |
| 2628 | * |
| 2629 | * Do a copy of the node. |
| 2630 | * |
| 2631 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2632 | */ |
| 2633 | xmlNodePtr |
| 2634 | xmlCopyNode(xmlNodePtr node, int recursive) { |
| 2635 | xmlNodePtr ret; |
| 2636 | |
| 2637 | ret = xmlStaticCopyNode(node, NULL, NULL, recursive); |
| 2638 | return(ret); |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * xmlCopyNodeList: |
| 2643 | * @node: the first node in the list. |
| 2644 | * |
| 2645 | * Do a recursive copy of the node list. |
| 2646 | * |
| 2647 | * Returns: a new xmlNodePtr, or NULL in case of error. |
| 2648 | */ |
| 2649 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
| 2650 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
| 2651 | return(ret); |
| 2652 | } |
| 2653 | |
| 2654 | /** |
| 2655 | * xmlCopyElement: |
| 2656 | * @elem: the element |
| 2657 | * |
| 2658 | * Do a copy of the element definition. |
| 2659 | * |
| 2660 | * Returns: a new xmlElementPtr, or NULL in case of error. |
| 2661 | xmlElementPtr |
| 2662 | xmlCopyElement(xmlElementPtr elem) { |
| 2663 | xmlElementPtr ret; |
| 2664 | |
| 2665 | if (elem == NULL) return(NULL); |
| 2666 | ret = xmlNewDocElement(elem->doc, elem->ns, elem->name, elem->content); |
| 2667 | if (ret == NULL) return(NULL); |
| 2668 | if (!recursive) return(ret); |
| 2669 | if (elem->properties != NULL) |
| 2670 | ret->properties = xmlCopyPropList(elem->properties); |
| 2671 | |
| 2672 | if (elem->nsDef != NULL) |
| 2673 | ret->nsDef = xmlCopyNamespaceList(elem->nsDef); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2674 | if (elem->children != NULL) |
| 2675 | ret->children = xmlCopyElementList(elem->children); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2676 | return(ret); |
| 2677 | } |
| 2678 | */ |
| 2679 | |
| 2680 | /** |
| 2681 | * xmlCopyDtd: |
| 2682 | * @dtd: the dtd |
| 2683 | * |
| 2684 | * Do a copy of the dtd. |
| 2685 | * |
| 2686 | * Returns: a new xmlDtdPtr, or NULL in case of error. |
| 2687 | */ |
| 2688 | xmlDtdPtr |
| 2689 | xmlCopyDtd(xmlDtdPtr dtd) { |
| 2690 | xmlDtdPtr ret; |
| 2691 | |
| 2692 | if (dtd == NULL) return(NULL); |
| 2693 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
| 2694 | if (ret == NULL) return(NULL); |
| 2695 | if (dtd->entities != NULL) |
| 2696 | ret->entities = (void *) xmlCopyEntitiesTable( |
| 2697 | (xmlEntitiesTablePtr) dtd->entities); |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 2698 | if (dtd->notations != NULL) |
| 2699 | ret->notations = (void *) xmlCopyNotationTable( |
| 2700 | (xmlNotationTablePtr) dtd->notations); |
| 2701 | if (dtd->elements != NULL) |
| 2702 | ret->elements = (void *) xmlCopyElementTable( |
| 2703 | (xmlElementTablePtr) dtd->elements); |
| 2704 | if (dtd->attributes != NULL) |
| 2705 | ret->attributes = (void *) xmlCopyAttributeTable( |
| 2706 | (xmlAttributeTablePtr) dtd->attributes); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2707 | return(ret); |
| 2708 | } |
| 2709 | |
| 2710 | /** |
| 2711 | * xmlCopyDoc: |
| 2712 | * @doc: the document |
| 2713 | * @recursive: if 1 do a recursive copy. |
| 2714 | * |
| 2715 | * Do a copy of the document info. If recursive, the content tree will |
| 2716 | * be copied too as well as Dtd, namespaces and entities. |
| 2717 | * |
| 2718 | * Returns: a new xmlDocPtr, or NULL in case of error. |
| 2719 | */ |
| 2720 | xmlDocPtr |
| 2721 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
| 2722 | xmlDocPtr ret; |
| 2723 | |
| 2724 | if (doc == NULL) return(NULL); |
| 2725 | ret = xmlNewDoc(doc->version); |
| 2726 | if (ret == NULL) return(NULL); |
| 2727 | if (doc->name != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2728 | ret->name = xmlMemStrdup(doc->name); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2729 | if (doc->encoding != NULL) |
| 2730 | ret->encoding = xmlStrdup(doc->encoding); |
| 2731 | ret->compression = doc->compression; |
| 2732 | ret->standalone = doc->standalone; |
| 2733 | if (!recursive) return(ret); |
| 2734 | |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 2735 | if (doc->intSubset != NULL) |
| 2736 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2737 | if (doc->oldNs != NULL) |
| 2738 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2739 | if (doc->children != NULL) { |
| 2740 | xmlNodePtr tmp; |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 2741 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
| 2742 | (xmlNodePtr)ret); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2743 | ret->last = NULL; |
| 2744 | tmp = ret->children; |
| 2745 | while (tmp != NULL) { |
| 2746 | if (tmp->next == NULL) |
| 2747 | ret->last = tmp; |
| 2748 | tmp = tmp->next; |
| 2749 | } |
| 2750 | } |
Daniel Veillard | be36afe | 1998-11-27 06:39:50 +0000 | [diff] [blame] | 2751 | return(ret); |
| 2752 | } |
| 2753 | |
| 2754 | /************************************************************************ |
| 2755 | * * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 2756 | * Content access functions * |
| 2757 | * * |
| 2758 | ************************************************************************/ |
| 2759 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 2760 | /** |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2761 | * xmlDocGetRootElement: |
| 2762 | * @doc: the document |
| 2763 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2764 | * Get the root element of the document (doc->children is a list |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2765 | * containing possibly comments, PIs, etc ...). |
| 2766 | * |
| 2767 | * Returns the xmlNodePtr for the root or NULL |
| 2768 | */ |
| 2769 | xmlNodePtr |
| 2770 | xmlDocGetRootElement(xmlDocPtr doc) { |
| 2771 | xmlNodePtr ret; |
| 2772 | |
| 2773 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2774 | ret = doc->children; |
Daniel Veillard | 944b5ff | 1999-12-15 19:08:24 +0000 | [diff] [blame] | 2775 | while (ret != NULL) { |
| 2776 | if (ret->type == XML_ELEMENT_NODE) |
| 2777 | return(ret); |
| 2778 | ret = ret->next; |
| 2779 | } |
| 2780 | return(ret); |
| 2781 | } |
| 2782 | |
| 2783 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2784 | * xmlDocSetRootElement: |
| 2785 | * @doc: the document |
| 2786 | * @root: the new document root element |
| 2787 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2788 | * Set the root element of the document (doc->children is a list |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2789 | * containing possibly comments, PIs, etc ...). |
| 2790 | * |
| 2791 | * Returns the old root element if any was found |
| 2792 | */ |
| 2793 | xmlNodePtr |
| 2794 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
| 2795 | xmlNodePtr old = NULL; |
| 2796 | |
| 2797 | if (doc == NULL) return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2798 | old = doc->children; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2799 | while (old != NULL) { |
| 2800 | if (old->type == XML_ELEMENT_NODE) |
| 2801 | break; |
| 2802 | old = old->next; |
| 2803 | } |
| 2804 | if (old == NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2805 | if (doc->children == NULL) { |
| 2806 | doc->children = root; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2807 | doc->last = root; |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2808 | } else { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2809 | xmlAddSibling(doc->children, root); |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2810 | } |
| 2811 | } else { |
| 2812 | xmlReplaceNode(old, root); |
| 2813 | } |
| 2814 | return(old); |
| 2815 | } |
| 2816 | |
| 2817 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2818 | * xmlNodeSetLang: |
| 2819 | * @cur: the node being changed |
| 2820 | * @lang: the langage description |
| 2821 | * |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2822 | * Set the language of a node, i.e. the values of the xml:lang |
| 2823 | * attribute. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2824 | */ |
| 2825 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 2826 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2827 | if (cur == NULL) return; |
| 2828 | switch(cur->type) { |
| 2829 | case XML_TEXT_NODE: |
| 2830 | case XML_CDATA_SECTION_NODE: |
| 2831 | case XML_COMMENT_NODE: |
| 2832 | case XML_DOCUMENT_NODE: |
| 2833 | case XML_DOCUMENT_TYPE_NODE: |
| 2834 | case XML_DOCUMENT_FRAG_NODE: |
| 2835 | case XML_NOTATION_NODE: |
| 2836 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2837 | case XML_DTD_NODE: |
| 2838 | case XML_ELEMENT_DECL: |
| 2839 | case XML_ATTRIBUTE_DECL: |
| 2840 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2841 | case XML_PI_NODE: |
| 2842 | case XML_ENTITY_REF_NODE: |
| 2843 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2844 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2845 | #ifdef LIBXML_SGML_ENABLED |
| 2846 | case XML_SGML_DOCUMENT_NODE: |
| 2847 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2848 | case XML_XINCLUDE_START: |
| 2849 | case XML_XINCLUDE_END: |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | case XML_ELEMENT_NODE: |
| 2852 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2853 | break; |
| 2854 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2855 | xmlSetProp(cur, BAD_CAST "xml:lang", lang); |
| 2856 | } |
| 2857 | |
| 2858 | /** |
| 2859 | * xmlNodeGetLang: |
| 2860 | * @cur: the node being checked |
| 2861 | * |
| 2862 | * Searches the language of a node, i.e. the values of the xml:lang |
| 2863 | * attribute or the one carried by the nearest ancestor. |
| 2864 | * |
| 2865 | * Returns a pointer to the lang value, or NULL if not found |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2866 | * It's up to the caller to free the memory. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2867 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2868 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2869 | xmlNodeGetLang(xmlNodePtr cur) { |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 2870 | xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2871 | |
| 2872 | while (cur != NULL) { |
| 2873 | lang = xmlGetProp(cur, BAD_CAST "xml:lang"); |
| 2874 | if (lang != NULL) |
| 2875 | return(lang); |
| 2876 | cur = cur->parent; |
| 2877 | } |
| 2878 | return(NULL); |
| 2879 | } |
| 2880 | |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2881 | |
| 2882 | /** |
| 2883 | * xmlNodeSetSpacePreserve: |
| 2884 | * @cur: the node being changed |
| 2885 | * @val: the xml:space value ("0": default, 1: "preserve") |
| 2886 | * |
| 2887 | * Set (or reset) the space preserving behaviour of a node, i.e. the |
| 2888 | * value of the xml:space attribute. |
| 2889 | */ |
| 2890 | void |
| 2891 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
| 2892 | if (cur == NULL) return; |
| 2893 | switch(cur->type) { |
| 2894 | case XML_TEXT_NODE: |
| 2895 | case XML_CDATA_SECTION_NODE: |
| 2896 | case XML_COMMENT_NODE: |
| 2897 | case XML_DOCUMENT_NODE: |
| 2898 | case XML_DOCUMENT_TYPE_NODE: |
| 2899 | case XML_DOCUMENT_FRAG_NODE: |
| 2900 | case XML_NOTATION_NODE: |
| 2901 | case XML_HTML_DOCUMENT_NODE: |
| 2902 | case XML_DTD_NODE: |
| 2903 | case XML_ELEMENT_DECL: |
| 2904 | case XML_ATTRIBUTE_DECL: |
| 2905 | case XML_ENTITY_DECL: |
| 2906 | case XML_PI_NODE: |
| 2907 | case XML_ENTITY_REF_NODE: |
| 2908 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2909 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2910 | case XML_XINCLUDE_START: |
| 2911 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2912 | #ifdef LIBXML_SGML_ENABLED |
| 2913 | case XML_SGML_DOCUMENT_NODE: |
| 2914 | #endif |
| 2915 | return; |
| 2916 | case XML_ELEMENT_NODE: |
| 2917 | case XML_ATTRIBUTE_NODE: |
| 2918 | break; |
| 2919 | } |
| 2920 | switch (val) { |
| 2921 | case 0: |
| 2922 | xmlSetProp(cur, BAD_CAST "xml:space", BAD_CAST "default"); |
| 2923 | break; |
| 2924 | case 1: |
| 2925 | xmlSetProp(cur, BAD_CAST "xml:space", |
| 2926 | BAD_CAST "preserve"); |
| 2927 | break; |
| 2928 | } |
| 2929 | } |
| 2930 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2931 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2932 | * xmlNodeGetSpacePreserve: |
| 2933 | * @cur: the node being checked |
| 2934 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2935 | * Searches the space preserving behaviour of a node, i.e. the values |
| 2936 | * of the xml:space attribute or the one carried by the nearest |
| 2937 | * ancestor. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2938 | * |
| 2939 | * Returns -1 if xml:space is not inheried, 0 if "default", 1 if "preserve" |
| 2940 | */ |
| 2941 | int |
| 2942 | xmlNodeGetSpacePreserve(xmlNodePtr cur) { |
| 2943 | xmlChar *space; |
| 2944 | |
| 2945 | while (cur != NULL) { |
| 2946 | space = xmlGetProp(cur, BAD_CAST "xml:space"); |
| 2947 | if (space != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2948 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2949 | xmlFree(space); |
| 2950 | return(1); |
| 2951 | } |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2952 | if (xmlStrEqual(space, BAD_CAST "default")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2953 | xmlFree(space); |
| 2954 | return(0); |
| 2955 | } |
| 2956 | xmlFree(space); |
| 2957 | } |
| 2958 | cur = cur->parent; |
| 2959 | } |
| 2960 | return(-1); |
| 2961 | } |
| 2962 | |
| 2963 | /** |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2964 | * xmlNodeSetName: |
| 2965 | * @cur: the node being changed |
| 2966 | * @name: the new tag name |
| 2967 | * |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 2968 | * Set (or reset) the name of a node. |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2969 | */ |
| 2970 | void |
| 2971 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
| 2972 | if (cur == NULL) return; |
| 2973 | if (name == NULL) return; |
| 2974 | switch(cur->type) { |
| 2975 | case XML_TEXT_NODE: |
| 2976 | case XML_CDATA_SECTION_NODE: |
| 2977 | case XML_COMMENT_NODE: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2978 | case XML_DOCUMENT_TYPE_NODE: |
| 2979 | case XML_DOCUMENT_FRAG_NODE: |
| 2980 | case XML_NOTATION_NODE: |
| 2981 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2982 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2983 | case XML_XINCLUDE_START: |
| 2984 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2985 | #ifdef LIBXML_SGML_ENABLED |
| 2986 | case XML_SGML_DOCUMENT_NODE: |
| 2987 | #endif |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2988 | return; |
| 2989 | case XML_ELEMENT_NODE: |
| 2990 | case XML_ATTRIBUTE_NODE: |
| 2991 | case XML_PI_NODE: |
| 2992 | case XML_ENTITY_REF_NODE: |
| 2993 | case XML_ENTITY_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2994 | case XML_DTD_NODE: |
| 2995 | case XML_DOCUMENT_NODE: |
| 2996 | case XML_ELEMENT_DECL: |
| 2997 | case XML_ATTRIBUTE_DECL: |
| 2998 | case XML_ENTITY_DECL: |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | } |
| 3001 | if (cur->name != NULL) xmlFree((xmlChar *) cur->name); |
| 3002 | cur->name = xmlStrdup(name); |
| 3003 | } |
| 3004 | |
| 3005 | /** |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 3006 | * xmlNodeSetBase: |
| 3007 | * @cur: the node being changed |
| 3008 | * @uri: the new base URI |
| 3009 | * |
| 3010 | * Set (or reset) the base URI of a node, i.e. the value of the |
| 3011 | * xml:base attribute. |
| 3012 | */ |
| 3013 | void |
| 3014 | xmlNodeSetBase(xmlNodePtr cur, xmlChar* uri) { |
| 3015 | if (cur == NULL) return; |
| 3016 | switch(cur->type) { |
| 3017 | case XML_TEXT_NODE: |
| 3018 | case XML_CDATA_SECTION_NODE: |
| 3019 | case XML_COMMENT_NODE: |
| 3020 | case XML_DOCUMENT_NODE: |
| 3021 | case XML_DOCUMENT_TYPE_NODE: |
| 3022 | case XML_DOCUMENT_FRAG_NODE: |
| 3023 | case XML_NOTATION_NODE: |
| 3024 | case XML_HTML_DOCUMENT_NODE: |
| 3025 | case XML_DTD_NODE: |
| 3026 | case XML_ELEMENT_DECL: |
| 3027 | case XML_ATTRIBUTE_DECL: |
| 3028 | case XML_ENTITY_DECL: |
| 3029 | case XML_PI_NODE: |
| 3030 | case XML_ENTITY_REF_NODE: |
| 3031 | case XML_ENTITY_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3032 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3033 | case XML_XINCLUDE_START: |
| 3034 | case XML_XINCLUDE_END: |
Daniel Veillard | be9ec4b | 2000-10-25 11:01:53 +0000 | [diff] [blame] | 3035 | #ifdef LIBXML_SGML_ENABLED |
| 3036 | case XML_SGML_DOCUMENT_NODE: |
| 3037 | #endif |
| 3038 | return; |
| 3039 | case XML_ELEMENT_NODE: |
| 3040 | case XML_ATTRIBUTE_NODE: |
| 3041 | break; |
| 3042 | } |
| 3043 | xmlSetProp(cur, BAD_CAST "xml:base", uri); |
| 3044 | } |
| 3045 | |
| 3046 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3047 | * xmlDocumentGetBase: |
| 3048 | * @doc: the document |
| 3049 | * |
| 3050 | * Searches for the Document BASE URL. The code should work on both XML |
| 3051 | * and HTML document. |
| 3052 | * It returns the base as defined in RFC 2396 section |
| 3053 | * 5.1.3. Base URI from the Retrieval URI |
| 3054 | * However it does not return the computed base (5.1.1 and 5.1.2), use |
| 3055 | * xmlNodeGetBase() for this |
| 3056 | * |
| 3057 | * Returns a pointer to the base URL, or NULL if not found |
| 3058 | * It's up to the caller to free the memory. |
| 3059 | */ |
| 3060 | xmlChar * |
| 3061 | xmlDocumentGetBase(xmlDocPtr doc) { |
| 3062 | if (doc == NULL) |
| 3063 | return(NULL); |
| 3064 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
| 3065 | if (doc->URL != NULL) |
| 3066 | return(xmlStrdup(doc->URL)); |
| 3067 | return(NULL); |
| 3068 | } |
| 3069 | if (doc->URL != NULL) |
| 3070 | return(xmlStrdup(doc->URL)); |
| 3071 | return(NULL); |
| 3072 | } |
| 3073 | |
| 3074 | /** |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3075 | * xmlNodeGetBase: |
| 3076 | * @doc: the document the node pertains to |
| 3077 | * @cur: the node being checked |
| 3078 | * |
| 3079 | * Searches for the BASE URL. The code should work on both XML |
| 3080 | * and HTML document even if base mechanisms are completely different. |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3081 | * It returns the base as defined in RFC 2396 sections |
| 3082 | * 5.1.1. Base URI within Document Content |
| 3083 | * and |
| 3084 | * 5.1.2. Base URI from the Encapsulating Entity |
| 3085 | * However it does not return the document base (5.1.3), use |
| 3086 | * xmlDocumentGetBase() for this |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3087 | * |
| 3088 | * Returns a pointer to the base URL, or NULL if not found |
| 3089 | * It's up to the caller to free the memory. |
| 3090 | */ |
| 3091 | xmlChar * |
| 3092 | xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { |
| 3093 | xmlChar *base; |
| 3094 | |
| 3095 | if ((cur == NULL) && (doc == NULL)) |
| 3096 | return(NULL); |
| 3097 | if (doc == NULL) doc = cur->doc; |
| 3098 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3099 | cur = doc->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3100 | while ((cur != NULL) && (cur->name != NULL)) { |
| 3101 | if (cur->type != XML_ELEMENT_NODE) { |
| 3102 | cur = cur->next; |
| 3103 | continue; |
| 3104 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3105 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3106 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3107 | continue; |
| 3108 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3109 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3110 | cur = cur->children; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3111 | continue; |
| 3112 | } |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 3113 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
| 3114 | return(xmlGetProp(cur, BAD_CAST "href")); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3115 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3116 | cur = cur->next; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3117 | } |
| 3118 | return(NULL); |
| 3119 | } |
| 3120 | while (cur != NULL) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3121 | if (cur->type == XML_ENTITY_DECL) { |
| 3122 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
| 3123 | return(xmlStrdup(ent->URI)); |
| 3124 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3125 | base = xmlGetProp(cur, BAD_CAST "xml:base"); |
| 3126 | if (base != NULL) |
| 3127 | return(base); |
| 3128 | cur = cur->parent; |
| 3129 | } |
| 3130 | return(NULL); |
| 3131 | } |
| 3132 | |
| 3133 | /** |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3134 | * xmlNodeGetContent: |
| 3135 | * @cur: the node being read |
| 3136 | * |
| 3137 | * Read the value of a node, this can be either the text carried |
| 3138 | * directly by this node if it's a TEXT node or the aggregate string |
| 3139 | * of the values carried by this node child's (TEXT and ENTITY_REF). |
| 3140 | * Entity references are substitued. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3141 | * Returns a new xmlChar * or NULL if no content is available. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 3142 | * It's up to the caller to free the memory. |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3143 | */ |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3144 | xmlChar * |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3145 | xmlNodeGetContent(xmlNodePtr cur) { |
| 3146 | if (cur == NULL) return(NULL); |
| 3147 | switch (cur->type) { |
| 3148 | case XML_DOCUMENT_FRAG_NODE: |
| 3149 | case XML_ELEMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3150 | return(xmlNodeListGetString(cur->doc, cur->children, 1)); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3151 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3152 | case XML_ATTRIBUTE_NODE: { |
| 3153 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3154 | if (attr->parent != NULL) |
| 3155 | return(xmlNodeListGetString(attr->parent->doc, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3156 | else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3157 | return(xmlNodeListGetString(NULL, attr->children, 1)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3158 | break; |
| 3159 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3160 | case XML_COMMENT_NODE: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3161 | case XML_PI_NODE: |
| 3162 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3163 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3164 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3165 | #else |
| 3166 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3167 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3168 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3169 | case XML_ENTITY_REF_NODE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3170 | /* |
| 3171 | * Locate the entity, and get it's content |
| 3172 | * @@@ |
| 3173 | */ |
| 3174 | return(NULL); |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3175 | case XML_ENTITY_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3176 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3177 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3178 | case XML_DOCUMENT_TYPE_NODE: |
| 3179 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3180 | case XML_DTD_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3181 | case XML_XINCLUDE_START: |
| 3182 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3183 | #ifdef LIBXML_SGML_ENABLED |
| 3184 | case XML_SGML_DOCUMENT_NODE: |
| 3185 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3186 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3187 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3188 | return(xmlStrdup(((xmlNsPtr)cur)->href)); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3189 | case XML_ELEMENT_DECL: |
| 3190 | /* TODO !!! */ |
| 3191 | return(NULL); |
| 3192 | case XML_ATTRIBUTE_DECL: |
| 3193 | /* TODO !!! */ |
| 3194 | return(NULL); |
| 3195 | case XML_ENTITY_DECL: |
| 3196 | /* TODO !!! */ |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3197 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3198 | case XML_CDATA_SECTION_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3199 | case XML_TEXT_NODE: |
| 3200 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3201 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3202 | return(xmlStrdup(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3203 | #else |
| 3204 | return(xmlStrdup(xmlBufferContent(cur->content))); |
| 3205 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3206 | return(NULL); |
| 3207 | } |
| 3208 | return(NULL); |
| 3209 | } |
| 3210 | |
| 3211 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3212 | * xmlNodeSetContent: |
| 3213 | * @cur: the node being modified |
| 3214 | * @content: the new value of the content |
| 3215 | * |
| 3216 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3217 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3218 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3219 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3220 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3221 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3222 | xmlGenericError(xmlGenericErrorContext, |
| 3223 | "xmlNodeSetContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3224 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3225 | return; |
| 3226 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3227 | switch (cur->type) { |
| 3228 | case XML_DOCUMENT_FRAG_NODE: |
| 3229 | case XML_ELEMENT_NODE: |
| 3230 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3231 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3232 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3233 | #else |
| 3234 | xmlBufferFree(cur->content); |
| 3235 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3236 | cur->content = NULL; |
| 3237 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3238 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3239 | cur->children = xmlStringGetNodeList(cur->doc, content); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3240 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3241 | break; |
| 3242 | case XML_ATTRIBUTE_NODE: |
| 3243 | break; |
| 3244 | case XML_TEXT_NODE: |
| 3245 | case XML_CDATA_SECTION_NODE: |
| 3246 | case XML_ENTITY_REF_NODE: |
| 3247 | case XML_ENTITY_NODE: |
| 3248 | case XML_PI_NODE: |
| 3249 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3250 | if (cur->content != NULL) { |
| 3251 | #ifndef XML_USE_BUFFER_CONTENT |
| 3252 | xmlFree(cur->content); |
| 3253 | #else |
| 3254 | xmlBufferFree(cur->content); |
| 3255 | #endif |
| 3256 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3257 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3258 | cur->last = cur->children = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3259 | if (content != NULL) { |
| 3260 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3261 | cur->content = xmlStrdup(content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3262 | #else |
| 3263 | cur->content = xmlBufferCreateSize(0); |
| 3264 | xmlBufferSetAllocationScheme(cur->content, |
| 3265 | xmlGetBufferAllocationScheme()); |
| 3266 | xmlBufferAdd(cur->content, content, -1); |
| 3267 | #endif |
| 3268 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3269 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3270 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3271 | case XML_DOCUMENT_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3272 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3273 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3274 | case XML_XINCLUDE_START: |
| 3275 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3276 | #ifdef LIBXML_SGML_ENABLED |
| 3277 | case XML_SGML_DOCUMENT_NODE: |
| 3278 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3279 | break; |
| 3280 | case XML_NOTATION_NODE: |
| 3281 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3282 | case XML_DTD_NODE: |
| 3283 | break; |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3284 | case XML_NAMESPACE_DECL: |
| 3285 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3286 | case XML_ELEMENT_DECL: |
| 3287 | /* TODO !!! */ |
| 3288 | break; |
| 3289 | case XML_ATTRIBUTE_DECL: |
| 3290 | /* TODO !!! */ |
| 3291 | break; |
| 3292 | case XML_ENTITY_DECL: |
| 3293 | /* TODO !!! */ |
| 3294 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3295 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3298 | /** |
| 3299 | * xmlNodeSetContentLen: |
| 3300 | * @cur: the node being modified |
| 3301 | * @content: the new value of the content |
| 3302 | * @len: the size of @content |
| 3303 | * |
| 3304 | * Replace the content of a node. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3305 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3306 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3307 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3308 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3309 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3310 | xmlGenericError(xmlGenericErrorContext, |
| 3311 | "xmlNodeSetContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3312 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3313 | return; |
| 3314 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3315 | switch (cur->type) { |
| 3316 | case XML_DOCUMENT_FRAG_NODE: |
| 3317 | case XML_ELEMENT_NODE: |
| 3318 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3319 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3320 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3321 | #else |
| 3322 | xmlBufferFree(cur->content); |
| 3323 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3324 | cur->content = NULL; |
| 3325 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3326 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3327 | cur->children = xmlStringLenGetNodeList(cur->doc, content, len); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3328 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3329 | break; |
| 3330 | case XML_ATTRIBUTE_NODE: |
| 3331 | break; |
| 3332 | case XML_TEXT_NODE: |
| 3333 | case XML_CDATA_SECTION_NODE: |
| 3334 | case XML_ENTITY_REF_NODE: |
| 3335 | case XML_ENTITY_NODE: |
| 3336 | case XML_PI_NODE: |
| 3337 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3338 | case XML_NOTATION_NODE: |
| 3339 | if (cur->content != NULL) { |
| 3340 | #ifndef XML_USE_BUFFER_CONTENT |
| 3341 | xmlFree(cur->content); |
| 3342 | #else |
| 3343 | xmlBufferFree(cur->content); |
| 3344 | #endif |
| 3345 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3346 | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
| 3347 | cur->children = cur->last = NULL; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3348 | if (content != NULL) { |
| 3349 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3350 | cur->content = xmlStrndup(content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3351 | #else |
| 3352 | cur->content = xmlBufferCreateSize(len); |
| 3353 | xmlBufferSetAllocationScheme(cur->content, |
| 3354 | xmlGetBufferAllocationScheme()); |
| 3355 | xmlBufferAdd(cur->content, content, len); |
| 3356 | #endif |
| 3357 | } else |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3358 | cur->content = NULL; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3359 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3360 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3361 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3362 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3363 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3364 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3365 | case XML_XINCLUDE_START: |
| 3366 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3367 | #ifdef LIBXML_SGML_ENABLED |
| 3368 | case XML_SGML_DOCUMENT_NODE: |
| 3369 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3370 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3371 | case XML_ELEMENT_DECL: |
| 3372 | /* TODO !!! */ |
| 3373 | break; |
| 3374 | case XML_ATTRIBUTE_DECL: |
| 3375 | /* TODO !!! */ |
| 3376 | break; |
| 3377 | case XML_ENTITY_DECL: |
| 3378 | /* TODO !!! */ |
| 3379 | break; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3380 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3383 | /** |
| 3384 | * xmlNodeAddContentLen: |
| 3385 | * @cur: the node being modified |
| 3386 | * @content: extra content |
| 3387 | * @len: the size of @content |
| 3388 | * |
| 3389 | * Append the extra substring to the node content. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3390 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3391 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3392 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3393 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3394 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3395 | xmlGenericError(xmlGenericErrorContext, |
| 3396 | "xmlNodeAddContentLen : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3397 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
| 3400 | if (len <= 0) return; |
| 3401 | switch (cur->type) { |
| 3402 | case XML_DOCUMENT_FRAG_NODE: |
| 3403 | case XML_ELEMENT_NODE: { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3404 | xmlNodePtr last = NULL, newNode; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3405 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3406 | if (cur->children != NULL) { |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3407 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3408 | } else { |
| 3409 | if (cur->content != NULL) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3410 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3411 | cur->children = xmlStringGetNodeList(cur->doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3412 | #else |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3413 | cur->children = xmlStringGetNodeList(cur->doc, |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3414 | xmlBufferContent(cur->content)); |
| 3415 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3416 | UPDATE_LAST_CHILD_AND_PARENT(cur) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3417 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3418 | xmlFree(cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3419 | #else |
| 3420 | xmlBufferFree(cur->content); |
| 3421 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3422 | cur->content = NULL; |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3423 | last = cur->last; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3424 | } |
| 3425 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3426 | newNode = xmlNewTextLen(content, len); |
| 3427 | if (newNode != NULL) { |
| 3428 | xmlAddChild(cur, newNode); |
| 3429 | if ((last != NULL) && (last->next == newNode)) { |
| 3430 | xmlTextMerge(last, newNode); |
Daniel Veillard | 39a1f9a | 1999-01-17 19:11:59 +0000 | [diff] [blame] | 3431 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3432 | } |
| 3433 | break; |
| 3434 | } |
| 3435 | case XML_ATTRIBUTE_NODE: |
| 3436 | break; |
| 3437 | case XML_TEXT_NODE: |
| 3438 | case XML_CDATA_SECTION_NODE: |
| 3439 | case XML_ENTITY_REF_NODE: |
| 3440 | case XML_ENTITY_NODE: |
| 3441 | case XML_PI_NODE: |
| 3442 | case XML_COMMENT_NODE: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3443 | case XML_NOTATION_NODE: |
| 3444 | if (content != NULL) { |
| 3445 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3446 | cur->content = xmlStrncat(cur->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3447 | #else |
| 3448 | xmlBufferAdd(cur->content, content, len); |
| 3449 | #endif |
| 3450 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3451 | case XML_DOCUMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3452 | case XML_DTD_NODE: |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 3453 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3454 | case XML_DOCUMENT_TYPE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 3455 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3456 | case XML_XINCLUDE_START: |
| 3457 | case XML_XINCLUDE_END: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 3458 | #ifdef LIBXML_SGML_ENABLED |
| 3459 | case XML_SGML_DOCUMENT_NODE: |
| 3460 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3461 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3462 | case XML_ELEMENT_DECL: |
| 3463 | case XML_ATTRIBUTE_DECL: |
| 3464 | case XML_ENTITY_DECL: |
| 3465 | break; |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | /** |
| 3470 | * xmlNodeAddContent: |
| 3471 | * @cur: the node being modified |
| 3472 | * @content: extra content |
| 3473 | * |
| 3474 | * Append the extra substring to the node content. |
| 3475 | */ |
| 3476 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3477 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3478 | int len; |
| 3479 | |
| 3480 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3481 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3482 | xmlGenericError(xmlGenericErrorContext, |
| 3483 | "xmlNodeAddContent : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 3484 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3485 | return; |
| 3486 | } |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3487 | if (content == NULL) return; |
| 3488 | len = xmlStrlen(content); |
| 3489 | xmlNodeAddContentLen(cur, content, len); |
| 3490 | } |
| 3491 | |
| 3492 | /** |
| 3493 | * xmlTextMerge: |
| 3494 | * @first: the first text node |
| 3495 | * @second: the second text node being merged |
| 3496 | * |
| 3497 | * Merge two text nodes into one |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3498 | * Returns the first text node augmented |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3499 | */ |
| 3500 | xmlNodePtr |
| 3501 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
| 3502 | if (first == NULL) return(second); |
| 3503 | if (second == NULL) return(first); |
| 3504 | if (first->type != XML_TEXT_NODE) return(first); |
| 3505 | if (second->type != XML_TEXT_NODE) return(first); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3506 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3507 | xmlNodeAddContent(first, second->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 3508 | #else |
| 3509 | xmlNodeAddContent(first, xmlBufferContent(second->content)); |
| 3510 | #endif |
Daniel Veillard | 1625364 | 1998-10-28 22:58:05 +0000 | [diff] [blame] | 3511 | xmlUnlinkNode(second); |
| 3512 | xmlFreeNode(second); |
| 3513 | return(first); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3514 | } |
| 3515 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3516 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3517 | * xmlGetNsList: |
| 3518 | * @doc: the document |
| 3519 | * @node: the current node |
| 3520 | * |
| 3521 | * Search all the namespace applying to a given element. |
| 3522 | * Returns an NULL terminated array of all the xmlNsPtr found |
| 3523 | * that need to be freed by the caller or NULL if no |
| 3524 | * namespace if defined |
| 3525 | */ |
| 3526 | xmlNsPtr * |
| 3527 | xmlGetNsList(xmlDocPtr doc, xmlNodePtr node) { |
| 3528 | xmlNsPtr cur; |
| 3529 | xmlNsPtr *ret = NULL; |
| 3530 | int nbns = 0; |
| 3531 | int maxns = 10; |
| 3532 | int i; |
| 3533 | |
| 3534 | while (node != NULL) { |
| 3535 | cur = node->nsDef; |
| 3536 | while (cur != NULL) { |
| 3537 | if (ret == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3538 | ret = (xmlNsPtr *) xmlMalloc((maxns + 1) * sizeof(xmlNsPtr)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3539 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3540 | xmlGenericError(xmlGenericErrorContext, |
| 3541 | "xmlGetNsList : out of memory!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3542 | return(NULL); |
| 3543 | } |
| 3544 | ret[nbns] = NULL; |
| 3545 | } |
| 3546 | for (i = 0;i < nbns;i++) { |
| 3547 | if ((cur->prefix == ret[i]->prefix) || |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3548 | (xmlStrEqual(cur->prefix, ret[i]->prefix))) break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3549 | } |
| 3550 | if (i >= nbns) { |
| 3551 | if (nbns >= maxns) { |
| 3552 | maxns *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3553 | ret = (xmlNsPtr *) xmlRealloc(ret, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3554 | (maxns + 1) * sizeof(xmlNsPtr)); |
| 3555 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3556 | xmlGenericError(xmlGenericErrorContext, |
| 3557 | "xmlGetNsList : realloc failed!\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3558 | return(NULL); |
| 3559 | } |
| 3560 | } |
| 3561 | ret[nbns++] = cur; |
| 3562 | ret[nbns] = NULL; |
| 3563 | } |
| 3564 | |
| 3565 | cur = cur->next; |
| 3566 | } |
| 3567 | node = node->parent; |
| 3568 | } |
| 3569 | return(ret); |
| 3570 | } |
| 3571 | |
| 3572 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3573 | * xmlSearchNs: |
| 3574 | * @doc: the document |
| 3575 | * @node: the current node |
| 3576 | * @nameSpace: the namespace string |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3577 | * |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3578 | * Search a Ns registered under a given name space for a document. |
| 3579 | * recurse on the parents until it finds the defined namespace |
| 3580 | * or return NULL otherwise. |
| 3581 | * @nameSpace can be NULL, this is a search for the default namespace. |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3582 | * We don't allow to cross entities boundaries. If you don't declare |
| 3583 | * the namespace within those you will be in troubles !!! A warning |
| 3584 | * is generated to cover this case. |
| 3585 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3586 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3587 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3588 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3589 | xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3590 | xmlNsPtr cur; |
| 3591 | |
Daniel Veillard | 62ba71e | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 3592 | if (node == NULL) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3593 | while (node != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3594 | if ((node->type == XML_ENTITY_REF_NODE) || |
| 3595 | (node->type == XML_ENTITY_NODE) || |
| 3596 | (node->type == XML_ENTITY_DECL)) |
| 3597 | return(NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3598 | if (node->type == XML_ELEMENT_NODE) { |
| 3599 | cur = node->nsDef; |
| 3600 | while (cur != NULL) { |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3601 | if ((cur->prefix == NULL) && (nameSpace == NULL) && |
| 3602 | (cur->href != NULL)) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3603 | return(cur); |
| 3604 | if ((cur->prefix != NULL) && (nameSpace != NULL) && |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 3605 | (cur->href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3606 | (xmlStrEqual(cur->prefix, nameSpace))) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3607 | return(cur); |
| 3608 | cur = cur->next; |
| 3609 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3610 | } |
| 3611 | node = node->parent; |
| 3612 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3613 | return(NULL); |
| 3614 | } |
| 3615 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3616 | /** |
| 3617 | * xmlSearchNsByHref: |
| 3618 | * @doc: the document |
| 3619 | * @node: the current node |
| 3620 | * @href: the namespace value |
| 3621 | * |
| 3622 | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
| 3623 | * the defined namespace or return NULL otherwise. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3624 | * Returns the namespace pointer or NULL. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3625 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 3626 | xmlNsPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3627 | xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar *href) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3628 | xmlNsPtr cur; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3629 | xmlNodePtr orig = node; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3630 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3631 | if ((node == NULL) || (href == NULL)) return(NULL); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3632 | while (node != NULL) { |
| 3633 | cur = node->nsDef; |
| 3634 | while (cur != NULL) { |
| 3635 | if ((cur->href != NULL) && (href != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3636 | (xmlStrEqual(cur->href, href))) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3637 | /* |
| 3638 | * Check that the prefix is not shadowed between orig and node |
| 3639 | */ |
| 3640 | xmlNodePtr check = orig; |
| 3641 | xmlNsPtr tst; |
| 3642 | |
| 3643 | while (check != node) { |
| 3644 | tst = check->nsDef; |
| 3645 | while (tst != NULL) { |
| 3646 | if ((tst->prefix == NULL) && (cur->prefix == NULL)) |
| 3647 | goto shadowed; |
| 3648 | if ((tst->prefix != NULL) && (cur->prefix != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3649 | (xmlStrEqual(tst->prefix, cur->prefix))) |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3650 | goto shadowed; |
| 3651 | tst = tst->next; |
| 3652 | } |
Daniel Veillard | c4f4f0b | 2000-10-29 17:46:30 +0000 | [diff] [blame] | 3653 | check = check->parent; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3654 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3655 | return(cur); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3656 | } |
| 3657 | shadowed: |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3658 | cur = cur->next; |
| 3659 | } |
| 3660 | node = node->parent; |
| 3661 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3662 | return(NULL); |
| 3663 | } |
| 3664 | |
| 3665 | /** |
| 3666 | * xmlNewReconciliedNs |
| 3667 | * @doc: the document |
| 3668 | * @tree: a node expected to hold the new namespace |
| 3669 | * @ns: the original namespace |
| 3670 | * |
| 3671 | * This function tries to locate a namespace definition in a tree |
| 3672 | * ancestors, or create a new namespace definition node similar to |
| 3673 | * @ns trying to reuse the same prefix. However if the given prefix is |
| 3674 | * null (default namespace) or reused within the subtree defined by |
| 3675 | * @tree or on one of its ancestors then a new prefix is generated. |
| 3676 | * Returns the (new) namespace definition or NULL in case of error |
| 3677 | */ |
| 3678 | xmlNsPtr |
| 3679 | xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) { |
| 3680 | xmlNsPtr def; |
| 3681 | xmlChar prefix[50]; |
| 3682 | int counter = 1; |
| 3683 | |
| 3684 | if (tree == NULL) { |
| 3685 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3686 | xmlGenericError(xmlGenericErrorContext, |
| 3687 | "xmlNewReconciliedNs : tree == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3688 | #endif |
| 3689 | return(NULL); |
| 3690 | } |
| 3691 | if (ns == NULL) { |
| 3692 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3693 | xmlGenericError(xmlGenericErrorContext, |
| 3694 | "xmlNewReconciliedNs : ns == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3695 | #endif |
| 3696 | return(NULL); |
| 3697 | } |
| 3698 | /* |
| 3699 | * Search an existing namespace definition inherited. |
| 3700 | */ |
| 3701 | def = xmlSearchNsByHref(doc, tree, ns->href); |
| 3702 | if (def != NULL) |
| 3703 | return(def); |
| 3704 | |
| 3705 | /* |
| 3706 | * Find a close prefix which is not already in use. |
| 3707 | * Let's strip namespace prefixes longer than 20 chars ! |
| 3708 | */ |
| 3709 | sprintf((char *) prefix, "%.20s", ns->prefix); |
| 3710 | def = xmlSearchNs(doc, tree, prefix); |
| 3711 | while (def != NULL) { |
| 3712 | if (counter > 1000) return(NULL); |
| 3713 | sprintf((char *) prefix, "%.20s%d", ns->prefix, counter++); |
| 3714 | def = xmlSearchNs(doc, tree, prefix); |
| 3715 | } |
| 3716 | |
| 3717 | /* |
| 3718 | * Ok, now we are ready to create a new one. |
| 3719 | */ |
| 3720 | def = xmlNewNs(tree, ns->href, prefix); |
| 3721 | return(def); |
| 3722 | } |
| 3723 | |
| 3724 | /** |
| 3725 | * xmlReconciliateNs |
| 3726 | * @doc: the document |
| 3727 | * @tree: a node defining the subtree to reconciliate |
| 3728 | * |
| 3729 | * This function checks that all the namespaces declared within the given |
| 3730 | * tree are properly declared. This is needed for example after Copy or Cut |
| 3731 | * and then paste operations. The subtree may still hold pointers to |
| 3732 | * namespace declarations outside the subtree or invalid/masked. As much |
| 3733 | * as possible the function try tu reuse the existing namespaces found in |
| 3734 | * the new environment. If not possible the new namespaces are redeclared |
| 3735 | * on @tree at the top of the given subtree. |
| 3736 | * Returns the number of namespace declarations created or -1 in case of error. |
| 3737 | */ |
| 3738 | int |
| 3739 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 3740 | xmlNsPtr *oldNs = NULL; |
| 3741 | xmlNsPtr *newNs = NULL; |
| 3742 | int sizeCache = 0; |
| 3743 | int nbCache = 0; |
| 3744 | |
| 3745 | xmlNsPtr n; |
| 3746 | xmlNodePtr node = tree; |
| 3747 | xmlAttrPtr attr; |
| 3748 | int ret = 0, i; |
| 3749 | |
| 3750 | while (node != NULL) { |
| 3751 | /* |
| 3752 | * Reconciliate the node namespace |
| 3753 | */ |
| 3754 | if (node->ns != NULL) { |
| 3755 | /* |
| 3756 | * initialize the cache if needed |
| 3757 | */ |
| 3758 | if (sizeCache == 0) { |
| 3759 | sizeCache = 10; |
| 3760 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3761 | sizeof(xmlNsPtr)); |
| 3762 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3763 | xmlGenericError(xmlGenericErrorContext, |
| 3764 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3765 | return(-1); |
| 3766 | } |
| 3767 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3768 | sizeof(xmlNsPtr)); |
| 3769 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3770 | xmlGenericError(xmlGenericErrorContext, |
| 3771 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3772 | xmlFree(oldNs); |
| 3773 | return(-1); |
| 3774 | } |
| 3775 | } |
| 3776 | for (i = 0;i < nbCache;i++) { |
| 3777 | if (oldNs[i] == node->ns) { |
| 3778 | node->ns = newNs[i]; |
| 3779 | break; |
| 3780 | } |
| 3781 | } |
| 3782 | if (i == nbCache) { |
| 3783 | /* |
| 3784 | * Ok we need to recreate a new namespace definition |
| 3785 | */ |
| 3786 | n = xmlNewReconciliedNs(doc, tree, node->ns); |
| 3787 | if (n != NULL) { /* :-( what if else ??? */ |
| 3788 | /* |
| 3789 | * check if we need to grow the cache buffers. |
| 3790 | */ |
| 3791 | if (sizeCache <= nbCache) { |
| 3792 | sizeCache *= 2; |
| 3793 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3794 | sizeof(xmlNsPtr)); |
| 3795 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3796 | xmlGenericError(xmlGenericErrorContext, |
| 3797 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3798 | xmlFree(newNs); |
| 3799 | return(-1); |
| 3800 | } |
| 3801 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3802 | sizeof(xmlNsPtr)); |
| 3803 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3804 | xmlGenericError(xmlGenericErrorContext, |
| 3805 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3806 | xmlFree(oldNs); |
| 3807 | return(-1); |
| 3808 | } |
| 3809 | } |
| 3810 | newNs[nbCache] = n; |
| 3811 | oldNs[nbCache++] = node->ns; |
| 3812 | node->ns = n; |
| 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | /* |
| 3817 | * now check for namespace hold by attributes on the node. |
| 3818 | */ |
| 3819 | attr = node->properties; |
| 3820 | while (attr != NULL) { |
| 3821 | if (attr->ns != NULL) { |
| 3822 | /* |
| 3823 | * initialize the cache if needed |
| 3824 | */ |
| 3825 | if (sizeCache == 0) { |
| 3826 | sizeCache = 10; |
| 3827 | oldNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3828 | sizeof(xmlNsPtr)); |
| 3829 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3830 | xmlGenericError(xmlGenericErrorContext, |
| 3831 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3832 | return(-1); |
| 3833 | } |
| 3834 | newNs = (xmlNsPtr *) xmlMalloc(sizeCache * |
| 3835 | sizeof(xmlNsPtr)); |
| 3836 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3837 | xmlGenericError(xmlGenericErrorContext, |
| 3838 | "xmlReconciliateNs : memory pbm\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3839 | xmlFree(oldNs); |
| 3840 | return(-1); |
| 3841 | } |
| 3842 | } |
| 3843 | for (i = 0;i < nbCache;i++) { |
| 3844 | if (oldNs[i] == attr->ns) { |
| 3845 | node->ns = newNs[i]; |
| 3846 | break; |
| 3847 | } |
| 3848 | } |
| 3849 | if (i == nbCache) { |
| 3850 | /* |
| 3851 | * Ok we need to recreate a new namespace definition |
| 3852 | */ |
| 3853 | n = xmlNewReconciliedNs(doc, tree, attr->ns); |
| 3854 | if (n != NULL) { /* :-( what if else ??? */ |
| 3855 | /* |
| 3856 | * check if we need to grow the cache buffers. |
| 3857 | */ |
| 3858 | if (sizeCache <= nbCache) { |
| 3859 | sizeCache *= 2; |
| 3860 | oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache * |
| 3861 | sizeof(xmlNsPtr)); |
| 3862 | if (oldNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3863 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3864 | "xmlReconciliateNs : memory pbm\n"); |
| 3865 | xmlFree(newNs); |
| 3866 | return(-1); |
| 3867 | } |
| 3868 | newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache * |
| 3869 | sizeof(xmlNsPtr)); |
| 3870 | if (newNs == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3871 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3872 | "xmlReconciliateNs : memory pbm\n"); |
| 3873 | xmlFree(oldNs); |
| 3874 | return(-1); |
| 3875 | } |
| 3876 | } |
| 3877 | newNs[nbCache] = n; |
| 3878 | oldNs[nbCache++] = attr->ns; |
| 3879 | attr->ns = n; |
| 3880 | } |
| 3881 | } |
| 3882 | } |
| 3883 | attr = attr->next; |
| 3884 | } |
| 3885 | |
| 3886 | /* |
| 3887 | * Browse the full subtree, deep first |
| 3888 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3889 | if (node->children != NULL) { |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3890 | /* deep first */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3891 | node = node->children; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3892 | } else if ((node != tree) && (node->next != NULL)) { |
| 3893 | /* then siblings */ |
| 3894 | node = node->next; |
| 3895 | } else if (node != tree) { |
| 3896 | /* go up to parents->next if needed */ |
| 3897 | while (node != tree) { |
| 3898 | if (node->parent != NULL) |
| 3899 | node = node->parent; |
| 3900 | if ((node != tree) && (node->next != NULL)) { |
| 3901 | node = node->next; |
| 3902 | break; |
| 3903 | } |
| 3904 | if (node->parent == NULL) { |
| 3905 | node = NULL; |
| 3906 | break; |
| 3907 | } |
| 3908 | } |
| 3909 | /* exit condition */ |
| 3910 | if (node == tree) |
| 3911 | node = NULL; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3914 | return(ret); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3917 | /** |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3918 | * xmlHasProp: |
| 3919 | * @node: the node |
| 3920 | * @name: the attribute name |
| 3921 | * |
| 3922 | * Search an attribute associated to a node |
| 3923 | * This function also looks in DTD attribute declaration for #FIXED or |
| 3924 | * default declaration values unless DTD use has been turned off. |
| 3925 | * |
| 3926 | * Returns the attribute or the attribute declaration or NULL if |
| 3927 | * neither was found. |
| 3928 | */ |
| 3929 | xmlAttrPtr |
| 3930 | xmlHasProp(xmlNodePtr node, const xmlChar *name) { |
| 3931 | xmlAttrPtr prop; |
| 3932 | xmlDocPtr doc; |
| 3933 | |
| 3934 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3935 | /* |
| 3936 | * Check on the properties attached to the node |
| 3937 | */ |
| 3938 | prop = node->properties; |
| 3939 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3940 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3941 | return(prop); |
| 3942 | } |
| 3943 | prop = prop->next; |
| 3944 | } |
| 3945 | if (!xmlCheckDTD) return(NULL); |
| 3946 | |
| 3947 | /* |
| 3948 | * Check if there is a default declaration in the internal |
| 3949 | * or external subsets |
| 3950 | */ |
| 3951 | doc = node->doc; |
| 3952 | if (doc != NULL) { |
| 3953 | xmlAttributePtr attrDecl; |
| 3954 | if (doc->intSubset != NULL) { |
| 3955 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 3956 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 3957 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 3958 | if (attrDecl != NULL) |
| 3959 | return((xmlAttrPtr) attrDecl); |
| 3960 | } |
| 3961 | } |
| 3962 | return(NULL); |
| 3963 | } |
| 3964 | |
| 3965 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 3966 | * xmlGetProp: |
| 3967 | * @node: the node |
| 3968 | * @name: the attribute name |
| 3969 | * |
| 3970 | * Search and get the value of an attribute associated to a node |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 3971 | * This does the entity substitution. |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3972 | * This function looks in DTD attribute declaration for #FIXED or |
| 3973 | * default declaration values unless DTD use has been turned off. |
| 3974 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 3975 | * Returns the attribute value or NULL if not found. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3976 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3977 | */ |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 3978 | xmlChar * |
| 3979 | xmlGetProp(xmlNodePtr node, const xmlChar *name) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3980 | xmlAttrPtr prop; |
| 3981 | xmlDocPtr doc; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3982 | |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3983 | if ((node == NULL) || (name == NULL)) return(NULL); |
| 3984 | /* |
| 3985 | * Check on the properties attached to the node |
| 3986 | */ |
| 3987 | prop = node->properties; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3988 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3989 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3990 | xmlChar *ret; |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3991 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3992 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3993 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
Daniel Veillard | 6800ef3 | 1999-02-08 18:33:22 +0000 | [diff] [blame] | 3994 | return(ret); |
Daniel Veillard | 6817893 | 1999-02-08 18:34:36 +0000 | [diff] [blame] | 3995 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 3996 | prop = prop->next; |
| 3997 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 3998 | if (!xmlCheckDTD) return(NULL); |
| 3999 | |
| 4000 | /* |
| 4001 | * Check if there is a default declaration in the internal |
| 4002 | * or external subsets |
| 4003 | */ |
| 4004 | doc = node->doc; |
| 4005 | if (doc != NULL) { |
| 4006 | xmlAttributePtr attrDecl; |
| 4007 | if (doc->intSubset != NULL) { |
| 4008 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4009 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4010 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
Daniel Veillard | f967b90 | 2000-01-17 16:06:10 +0000 | [diff] [blame] | 4011 | if (attrDecl != NULL) |
| 4012 | return(xmlStrdup(attrDecl->defaultValue)); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | return(NULL); |
| 4016 | } |
| 4017 | |
| 4018 | /** |
| 4019 | * xmlGetNsProp: |
| 4020 | * @node: the node |
| 4021 | * @name: the attribute name |
| 4022 | * @namespace: the URI of the namespace |
| 4023 | * |
| 4024 | * Search and get the value of an attribute associated to a node |
| 4025 | * This attribute has to be anchored in the namespace specified. |
| 4026 | * This does the entity substitution. |
| 4027 | * This function looks in DTD attribute declaration for #FIXED or |
| 4028 | * default declaration values unless DTD use has been turned off. |
| 4029 | * |
| 4030 | * Returns the attribute value or NULL if not found. |
| 4031 | * It's up to the caller to free the memory. |
| 4032 | */ |
| 4033 | xmlChar * |
| 4034 | xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *namespace) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 4035 | xmlAttrPtr prop; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4036 | xmlDocPtr doc; |
| 4037 | xmlNsPtr ns; |
| 4038 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 4039 | if (node == NULL) |
| 4040 | return(NULL); |
| 4041 | |
| 4042 | prop = node->properties; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4043 | if (namespace == NULL) |
| 4044 | return(xmlGetProp(node, name)); |
| 4045 | while (prop != NULL) { |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4046 | /* |
| 4047 | * One need to have |
| 4048 | * - same attribute names |
| 4049 | * - and the attribute carrying that namespace |
| 4050 | * or |
| 4051 | * no namespace on the attribute and the element carrying it |
| 4052 | */ |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4053 | if ((xmlStrEqual(prop->name, name)) && |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 4054 | (((prop->ns == NULL) && (node->ns != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4055 | (xmlStrEqual(node->ns->href, namespace))) || |
| 4056 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, namespace))))) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4057 | xmlChar *ret; |
| 4058 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4059 | ret = xmlNodeListGetString(node->doc, prop->children, 1); |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4060 | if (ret == NULL) return(xmlStrdup((xmlChar *)"")); |
| 4061 | return(ret); |
| 4062 | } |
| 4063 | prop = prop->next; |
| 4064 | } |
| 4065 | if (!xmlCheckDTD) return(NULL); |
| 4066 | |
| 4067 | /* |
| 4068 | * Check if there is a default declaration in the internal |
| 4069 | * or external subsets |
| 4070 | */ |
| 4071 | doc = node->doc; |
| 4072 | if (doc != NULL) { |
| 4073 | xmlAttributePtr attrDecl; |
| 4074 | if (doc->intSubset != NULL) { |
| 4075 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
| 4076 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
| 4077 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
| 4078 | |
Daniel Veillard | c2f4df2 | 2001-01-04 14:06:39 +0000 | [diff] [blame] | 4079 | if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) { |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4080 | /* |
| 4081 | * The DTD declaration only allows a prefix search |
| 4082 | */ |
| 4083 | ns = xmlSearchNs(doc, node, attrDecl->prefix); |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4084 | if ((ns != NULL) && (xmlStrEqual(ns->href, namespace))) |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4085 | return(xmlStrdup(attrDecl->defaultValue)); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4089 | return(NULL); |
| 4090 | } |
| 4091 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4092 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4093 | * xmlSetProp: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4094 | * @node: the node |
| 4095 | * @name: the attribute name |
| 4096 | * @value: the attribute value |
| 4097 | * |
| 4098 | * Set (or reset) an attribute carried by a node. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4099 | * Returns the attribute pointer. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4100 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4101 | xmlAttrPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4102 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4103 | xmlAttrPtr prop = node->properties; |
| 4104 | |
| 4105 | while (prop != NULL) { |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 4106 | if (xmlStrEqual(prop->name, name)) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4107 | if (prop->children != NULL) |
| 4108 | xmlFreeNodeList(prop->children); |
| 4109 | prop->children = NULL; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4110 | prop->last = NULL; |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4111 | if (value != NULL) { |
| 4112 | xmlChar *buffer; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4113 | xmlNodePtr tmp; |
| 4114 | |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4115 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4116 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4117 | prop->last = NULL; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4118 | tmp = prop->children; |
| 4119 | while (tmp != NULL) { |
| 4120 | tmp->parent = (xmlNodePtr) prop; |
| 4121 | if (tmp->next == NULL) |
| 4122 | prop->last = tmp; |
| 4123 | tmp = tmp->next; |
| 4124 | } |
Daniel Veillard | 51e3b15 | 1999-11-12 17:02:31 +0000 | [diff] [blame] | 4125 | xmlFree(buffer); |
| 4126 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4127 | return(prop); |
| 4128 | } |
| 4129 | prop = prop->next; |
| 4130 | } |
| 4131 | prop = xmlNewProp(node, name, value); |
| 4132 | return(prop); |
| 4133 | } |
| 4134 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4135 | /** |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 4136 | * xmlSetNsProp: |
| 4137 | * @node: the node |
| 4138 | * @ns: the namespace definition |
| 4139 | * @name: the attribute name |
| 4140 | * @value: the attribute value |
| 4141 | * |
| 4142 | * Set (or reset) an attribute carried by a node. |
| 4143 | * The ns structure must be in scope, this is not checked. |
| 4144 | * |
| 4145 | * Returns the attribute pointer. |
| 4146 | */ |
| 4147 | xmlAttrPtr |
| 4148 | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
| 4149 | const xmlChar *value) { |
| 4150 | xmlAttrPtr prop; |
| 4151 | |
| 4152 | if ((node == NULL) || (name == NULL)) |
| 4153 | return(NULL); |
| 4154 | |
| 4155 | if (ns == NULL) |
| 4156 | return(xmlSetProp(node, name, value)); |
| 4157 | if (ns->href == NULL) |
| 4158 | return(NULL); |
| 4159 | prop = node->properties; |
| 4160 | |
| 4161 | while (prop != NULL) { |
| 4162 | /* |
| 4163 | * One need to have |
| 4164 | * - same attribute names |
| 4165 | * - and the attribute carrying that namespace |
| 4166 | * or |
| 4167 | * no namespace on the attribute and the element carrying it |
| 4168 | */ |
| 4169 | if ((xmlStrEqual(prop->name, name)) && |
| 4170 | (((prop->ns == NULL) && (node->ns != NULL) && |
| 4171 | (xmlStrEqual(node->ns->href, ns->href))) || |
| 4172 | ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))))) { |
| 4173 | if (prop->children != NULL) |
| 4174 | xmlFreeNodeList(prop->children); |
| 4175 | prop->children = NULL; |
| 4176 | prop->last = NULL; |
| 4177 | prop->ns = ns; |
| 4178 | if (value != NULL) { |
| 4179 | xmlChar *buffer; |
| 4180 | xmlNodePtr tmp; |
| 4181 | |
| 4182 | buffer = xmlEncodeEntitiesReentrant(node->doc, value); |
| 4183 | prop->children = xmlStringGetNodeList(node->doc, buffer); |
| 4184 | prop->last = NULL; |
| 4185 | tmp = prop->children; |
| 4186 | while (tmp != NULL) { |
| 4187 | tmp->parent = (xmlNodePtr) prop; |
| 4188 | if (tmp->next == NULL) |
| 4189 | prop->last = tmp; |
| 4190 | tmp = tmp->next; |
| 4191 | } |
| 4192 | xmlFree(buffer); |
| 4193 | } |
| 4194 | return(prop); |
| 4195 | } |
| 4196 | prop = prop->next; |
| 4197 | } |
| 4198 | prop = xmlNewNsProp(node, ns, name, value); |
| 4199 | return(prop); |
| 4200 | } |
| 4201 | |
| 4202 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4203 | * xmlNodeIsText: |
| 4204 | * @node: the node |
| 4205 | * |
| 4206 | * Is this node a Text node ? |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4207 | * Returns 1 yes, 0 no |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4208 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4209 | int |
| 4210 | xmlNodeIsText(xmlNodePtr node) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4211 | if (node == NULL) return(0); |
| 4212 | |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 4213 | if (node->type == XML_TEXT_NODE) return(1); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4214 | return(0); |
| 4215 | } |
| 4216 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4217 | /** |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4218 | * xmlIsBlankNode: |
| 4219 | * @node: the node |
| 4220 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4221 | * Checks whether this node is an empty or whitespace only |
| 4222 | * (and possibly ignorable) text-node. |
| 4223 | * |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4224 | * Returns 1 yes, 0 no |
| 4225 | */ |
| 4226 | int |
| 4227 | xmlIsBlankNode(xmlNodePtr node) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4228 | const xmlChar *cur; |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4229 | if (node == NULL) return(0); |
| 4230 | |
| 4231 | if (node->type != XML_TEXT_NODE) return(0); |
Daniel Veillard | cd42961 | 2000-10-11 15:57:05 +0000 | [diff] [blame] | 4232 | if (node->content == NULL) return(1); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4233 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4234 | cur = node->content; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4235 | #else |
| 4236 | cur = xmlBufferContent(node->content); |
| 4237 | #endif |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 4238 | while (*cur != 0) { |
| 4239 | if (!IS_BLANK(*cur)) return(0); |
| 4240 | cur++; |
| 4241 | } |
| 4242 | |
| 4243 | return(1); |
| 4244 | } |
| 4245 | |
| 4246 | /** |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 4247 | * xmlTextConcat: |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4248 | * @node: the node |
| 4249 | * @content: the content |
| 4250 | * @len: @content lenght |
| 4251 | * |
| 4252 | * Concat the given string at the end of the existing node content |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4253 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4254 | |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4255 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4256 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4257 | if (node == NULL) return; |
| 4258 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4259 | if ((node->type != XML_TEXT_NODE) && |
| 4260 | (node->type != XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4261 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4262 | xmlGenericError(xmlGenericErrorContext, |
| 4263 | "xmlTextConcat: node is not text nor cdata\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4264 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4265 | return; |
| 4266 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4267 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4268 | node->content = xmlStrncat(node->content, content, len); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4269 | #else |
| 4270 | xmlBufferAdd(node->content, content, len); |
| 4271 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | /************************************************************************ |
| 4275 | * * |
| 4276 | * Output : to a FILE or in memory * |
| 4277 | * * |
| 4278 | ************************************************************************/ |
| 4279 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4280 | #define BASE_BUFFER_SIZE 4000 |
| 4281 | |
| 4282 | /** |
| 4283 | * xmlBufferCreate: |
| 4284 | * |
| 4285 | * routine to create an XML buffer. |
| 4286 | * returns the new structure. |
| 4287 | */ |
| 4288 | xmlBufferPtr |
| 4289 | xmlBufferCreate(void) { |
| 4290 | xmlBufferPtr ret; |
| 4291 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4292 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4293 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4294 | xmlGenericError(xmlGenericErrorContext, |
| 4295 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4296 | return(NULL); |
| 4297 | } |
| 4298 | ret->use = 0; |
| 4299 | ret->size = BASE_BUFFER_SIZE; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4300 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4301 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4302 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4303 | xmlGenericError(xmlGenericErrorContext, |
| 4304 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4305 | xmlFree(ret); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4306 | return(NULL); |
| 4307 | } |
| 4308 | ret->content[0] = 0; |
| 4309 | return(ret); |
| 4310 | } |
| 4311 | |
| 4312 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4313 | * xmlBufferCreateSize: |
| 4314 | * @size: initial size of buffer |
| 4315 | * |
| 4316 | * routine to create an XML buffer. |
| 4317 | * returns the new structure. |
| 4318 | */ |
| 4319 | xmlBufferPtr |
| 4320 | xmlBufferCreateSize(size_t size) { |
| 4321 | xmlBufferPtr ret; |
| 4322 | |
| 4323 | ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); |
| 4324 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4325 | xmlGenericError(xmlGenericErrorContext, |
| 4326 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4327 | return(NULL); |
| 4328 | } |
| 4329 | ret->use = 0; |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4330 | ret->alloc = xmlBufferAllocScheme; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4331 | ret->size = (size ? size+2 : 0); /* +1 for ending null */ |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4332 | if (ret->size){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4333 | ret->content = (xmlChar *) xmlMalloc(ret->size * sizeof(xmlChar)); |
| 4334 | if (ret->content == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4335 | xmlGenericError(xmlGenericErrorContext, |
| 4336 | "xmlBufferCreate : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4337 | xmlFree(ret); |
| 4338 | return(NULL); |
| 4339 | } |
| 4340 | ret->content[0] = 0; |
| 4341 | } else |
| 4342 | ret->content = NULL; |
| 4343 | return(ret); |
| 4344 | } |
| 4345 | |
| 4346 | /** |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 4347 | * xmlBufferSetAllocationScheme: |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4348 | * @buf: the buffer to free |
| 4349 | * @scheme: allocation scheme to use |
| 4350 | * |
| 4351 | * Sets the allocation scheme for this buffer |
| 4352 | */ |
| 4353 | void |
| 4354 | xmlBufferSetAllocationScheme(xmlBufferPtr buf, |
| 4355 | xmlBufferAllocationScheme scheme) { |
| 4356 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4357 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4358 | xmlGenericError(xmlGenericErrorContext, |
| 4359 | "xmlBufferSetAllocationScheme: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4360 | #endif |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4361 | return; |
| 4362 | } |
| 4363 | |
| 4364 | buf->alloc = scheme; |
| 4365 | } |
| 4366 | |
| 4367 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4368 | * xmlBufferFree: |
| 4369 | * @buf: the buffer to free |
| 4370 | * |
| 4371 | * Frees an XML buffer. |
| 4372 | */ |
| 4373 | void |
| 4374 | xmlBufferFree(xmlBufferPtr buf) { |
| 4375 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4376 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4377 | xmlGenericError(xmlGenericErrorContext, |
| 4378 | "xmlBufferFree: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4379 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4380 | return; |
| 4381 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4382 | if (buf->content != NULL) { |
| 4383 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4384 | memset(buf->content, -1, BASE_BUFFER_SIZE); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4385 | #else |
| 4386 | memset(buf->content, -1, buf->size); |
| 4387 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4388 | xmlFree(buf->content); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4389 | } |
| 4390 | memset(buf, -1, sizeof(xmlBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4391 | xmlFree(buf); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4395 | * xmlBufferEmpty: |
| 4396 | * @buf: the buffer |
| 4397 | * |
| 4398 | * empty a buffer. |
| 4399 | */ |
| 4400 | void |
| 4401 | xmlBufferEmpty(xmlBufferPtr buf) { |
Daniel Veillard | 4fb87ee | 2000-09-19 12:25:59 +0000 | [diff] [blame] | 4402 | if (buf->content == NULL) return; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4403 | buf->use = 0; |
| 4404 | memset(buf->content, -1, buf->size);/* just for debug */ |
| 4405 | } |
| 4406 | |
| 4407 | /** |
| 4408 | * xmlBufferShrink: |
| 4409 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4410 | * @len: the number of xmlChar to remove |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4411 | * |
| 4412 | * Remove the beginning of an XML buffer. |
| 4413 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4414 | * Returns the number of xmlChar removed, or -1 in case of failure. |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4415 | */ |
| 4416 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4417 | xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4418 | if (len == 0) return(0); |
| 4419 | if (len > buf->use) return(-1); |
| 4420 | |
| 4421 | buf->use -= len; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4422 | memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4423 | |
| 4424 | buf->content[buf->use] = 0; |
| 4425 | return(len); |
| 4426 | } |
| 4427 | |
| 4428 | /** |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4429 | * xmlBufferGrow: |
| 4430 | * @buf: the buffer |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 4431 | * @len: the minimum free size to allocate |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4432 | * |
| 4433 | * Grow the available space of an XML buffer. |
| 4434 | * |
| 4435 | * Returns the new available space or -1 in case of error |
| 4436 | */ |
| 4437 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4438 | xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4439 | int size; |
| 4440 | xmlChar *newbuf; |
| 4441 | |
Daniel Veillard | 4a6845d | 2001-01-03 13:32:39 +0000 | [diff] [blame] | 4442 | if (len + buf->use < buf->size) return(0); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4443 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4444 | size = buf->use + len + 100; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4445 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 4446 | newbuf = (xmlChar *) xmlRealloc(buf->content, size); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 4447 | if (newbuf == NULL) return(-1); |
| 4448 | buf->content = newbuf; |
| 4449 | buf->size = size; |
| 4450 | return(buf->size - buf->use); |
| 4451 | } |
| 4452 | |
| 4453 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4454 | * xmlBufferDump: |
| 4455 | * @file: the file output |
| 4456 | * @buf: the buffer to dump |
| 4457 | * |
| 4458 | * Dumps an XML buffer to a FILE *. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4459 | * Returns the number of xmlChar written |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4460 | */ |
| 4461 | int |
| 4462 | xmlBufferDump(FILE *file, xmlBufferPtr buf) { |
| 4463 | int ret; |
| 4464 | |
| 4465 | if (buf == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4466 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4467 | xmlGenericError(xmlGenericErrorContext, |
| 4468 | "xmlBufferDump: buf == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4469 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4470 | return(0); |
| 4471 | } |
| 4472 | if (buf->content == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4473 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4474 | xmlGenericError(xmlGenericErrorContext, |
| 4475 | "xmlBufferDump: buf->content == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4476 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4477 | return(0); |
| 4478 | } |
| 4479 | if (file == NULL) file = stdout; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4480 | ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4481 | return(ret); |
| 4482 | } |
| 4483 | |
| 4484 | /** |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4485 | * xmlBufferContent: |
| 4486 | * @buf: the buffer to resize |
| 4487 | * |
| 4488 | * Returns the internal content |
| 4489 | */ |
| 4490 | |
| 4491 | const xmlChar* |
| 4492 | xmlBufferContent(const xmlBufferPtr buf) |
| 4493 | { |
| 4494 | if(!buf) |
| 4495 | return NULL; |
| 4496 | |
| 4497 | return buf->content; |
| 4498 | } |
| 4499 | |
| 4500 | /** |
| 4501 | * xmlBufferLength: |
| 4502 | * @buf: the buffer |
| 4503 | * |
| 4504 | * Returns the length of data in the internal content |
| 4505 | */ |
| 4506 | |
| 4507 | int |
| 4508 | xmlBufferLength(const xmlBufferPtr buf) |
| 4509 | { |
| 4510 | if(!buf) |
| 4511 | return 0; |
| 4512 | |
| 4513 | return buf->use; |
| 4514 | } |
| 4515 | |
| 4516 | /** |
| 4517 | * xmlBufferResize: |
| 4518 | * @buf: the buffer to resize |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4519 | * @size: the desired size |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4520 | * |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4521 | * Resize a buffer to accomodate minimum size of @size. |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4522 | * |
| 4523 | * Returns 0 in case of problems, 1 otherwise |
| 4524 | */ |
| 4525 | int |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4526 | xmlBufferResize(xmlBufferPtr buf, unsigned int size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4527 | { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4528 | unsigned int newSize; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4529 | xmlChar* rebuf = NULL; |
| 4530 | |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4531 | /*take care of empty case*/ |
| 4532 | newSize = (buf->size ? buf->size*2 : size); |
| 4533 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4534 | /* Don't resize if we don't have to */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4535 | if (size < buf->size) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4536 | return 1; |
| 4537 | |
| 4538 | /* figure out new size */ |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4539 | switch (buf->alloc){ |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4540 | case XML_BUFFER_ALLOC_DOUBLEIT: |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4541 | while (size > newSize) newSize *= 2; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4542 | break; |
| 4543 | case XML_BUFFER_ALLOC_EXACT: |
| 4544 | newSize = size+10; |
| 4545 | break; |
| 4546 | default: |
| 4547 | newSize = size+10; |
| 4548 | break; |
| 4549 | } |
| 4550 | |
| 4551 | if (buf->content == NULL) |
| 4552 | rebuf = (xmlChar *) xmlMalloc(newSize * sizeof(xmlChar)); |
| 4553 | else |
| 4554 | rebuf = (xmlChar *) xmlRealloc(buf->content, |
| 4555 | newSize * sizeof(xmlChar)); |
| 4556 | if (rebuf == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4557 | xmlGenericError(xmlGenericErrorContext, |
| 4558 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4559 | return 0; |
| 4560 | } |
| 4561 | buf->content = rebuf; |
| 4562 | buf->size = newSize; |
| 4563 | |
| 4564 | return 1; |
| 4565 | } |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4566 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4567 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4568 | * xmlBufferAdd: |
| 4569 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4570 | * @str: the xmlChar string |
| 4571 | * @len: the number of xmlChar to add |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4572 | * |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4573 | * Add a string range to an XML buffer. if len == -1, the lenght of |
| 4574 | * str is recomputed. |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4575 | */ |
| 4576 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4577 | xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4578 | unsigned int needSize; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4579 | |
| 4580 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4581 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4582 | xmlGenericError(xmlGenericErrorContext, |
| 4583 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4584 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4585 | return; |
| 4586 | } |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4587 | if (len < -1) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4588 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4589 | xmlGenericError(xmlGenericErrorContext, |
| 4590 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4591 | #endif |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 4592 | return; |
| 4593 | } |
| 4594 | if (len == 0) return; |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4595 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4596 | if (len < 0) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4597 | len = xmlStrlen(str); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4598 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4599 | if (len <= 0) return; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4600 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4601 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4602 | if (needSize > buf->size){ |
| 4603 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4604 | xmlGenericError(xmlGenericErrorContext, |
| 4605 | "xmlBufferAdd : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4606 | return; |
| 4607 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4608 | } |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4609 | |
| 4610 | memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4611 | buf->use += len; |
| 4612 | buf->content[buf->use] = 0; |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4616 | * xmlBufferAddHead: |
| 4617 | * @buf: the buffer |
| 4618 | * @str: the xmlChar string |
| 4619 | * @len: the number of xmlChar to add |
| 4620 | * |
| 4621 | * Add a string range to the beginning of an XML buffer. |
| 4622 | * if len == -1, the lenght of @str is recomputed. |
| 4623 | */ |
| 4624 | void |
| 4625 | xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4626 | unsigned int needSize; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4627 | |
| 4628 | if (str == NULL) { |
| 4629 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4630 | xmlGenericError(xmlGenericErrorContext, |
| 4631 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4632 | #endif |
| 4633 | return; |
| 4634 | } |
| 4635 | if (len < -1) { |
| 4636 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4637 | xmlGenericError(xmlGenericErrorContext, |
| 4638 | "xmlBufferAdd: len < 0\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4639 | #endif |
| 4640 | return; |
| 4641 | } |
| 4642 | if (len == 0) return; |
| 4643 | |
| 4644 | if (len < 0) |
| 4645 | len = xmlStrlen(str); |
| 4646 | |
| 4647 | if (len <= 0) return; |
| 4648 | |
| 4649 | needSize = buf->use + len + 2; |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4650 | if (needSize > buf->size){ |
| 4651 | if (!xmlBufferResize(buf, needSize)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4652 | xmlGenericError(xmlGenericErrorContext, |
| 4653 | "xmlBufferAddHead : out of memory!\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4654 | return; |
| 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar)); |
| 4659 | memmove(&buf->content[0], str, len * sizeof(xmlChar)); |
| 4660 | buf->use += len; |
| 4661 | buf->content[buf->use] = 0; |
| 4662 | } |
| 4663 | |
| 4664 | /** |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4665 | * xmlBufferCat: |
| 4666 | * @buf: the buffer to dump |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4667 | * @str: the xmlChar string |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4668 | * |
| 4669 | * Append a zero terminated string to an XML buffer. |
| 4670 | */ |
| 4671 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4672 | xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4673 | if (str != NULL) |
| 4674 | xmlBufferAdd(buf, str, -1); |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | /** |
| 4678 | * xmlBufferCCat: |
| 4679 | * @buf: the buffer to dump |
| 4680 | * @str: the C char string |
| 4681 | * |
| 4682 | * Append a zero terminated C string to an XML buffer. |
| 4683 | */ |
| 4684 | void |
| 4685 | xmlBufferCCat(xmlBufferPtr buf, const char *str) { |
| 4686 | const char *cur; |
| 4687 | |
| 4688 | if (str == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4689 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4690 | xmlGenericError(xmlGenericErrorContext, |
| 4691 | "xmlBufferAdd: str == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4692 | #endif |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4693 | return; |
| 4694 | } |
| 4695 | for (cur = str;*cur != 0;cur++) { |
| 4696 | if (buf->use + 10 >= buf->size) { |
Daniel Veillard | 4b0755c | 2000-09-25 14:26:28 +0000 | [diff] [blame] | 4697 | if (!xmlBufferResize(buf, buf->use+10)){ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4698 | xmlGenericError(xmlGenericErrorContext, |
| 4699 | "xmlBufferCCat : out of memory!\n"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 4700 | return; |
| 4701 | } |
| 4702 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4703 | buf->content[buf->use++] = *cur; |
| 4704 | } |
| 4705 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4706 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4707 | /** |
| 4708 | * xmlBufferWriteCHAR: |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4709 | * @buf: the XML buffer |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4710 | * @string: the string to add |
| 4711 | * |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4712 | * routine which manages and grows an output buffer. This one adds |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4713 | * xmlChars at the end of the buffer. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4714 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4715 | void |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4716 | #ifdef VMS |
| 4717 | xmlBufferWriteXmlCHAR |
| 4718 | #else |
| 4719 | xmlBufferWriteCHAR |
| 4720 | #endif |
| 4721 | (xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4722 | xmlBufferCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4725 | /** |
| 4726 | * xmlBufferWriteChar: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4727 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4728 | * @string: the string to add |
| 4729 | * |
| 4730 | * routine which manage and grows an output buffer. This one add |
| 4731 | * C chars at the end of the array. |
| 4732 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4733 | void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4734 | xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { |
| 4735 | xmlBufferCCat(buf, string); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4738 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4739 | /** |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4740 | * xmlBufferWriteQuotedString: |
| 4741 | * @buf: the XML buffer output |
| 4742 | * @string: the string to add |
| 4743 | * |
| 4744 | * routine which manage and grows an output buffer. This one writes |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4745 | * a quoted or double quoted xmlChar string, checking first if it holds |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4746 | * quote or double-quotes internally |
| 4747 | */ |
| 4748 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4749 | xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4750 | if (xmlStrchr(string, '"')) { |
| 4751 | if (xmlStrchr(string, '\'')) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4752 | #ifdef DEBUG_BUFFER |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4753 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4754 | "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4755 | #endif |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4756 | } |
| 4757 | xmlBufferCCat(buf, "'"); |
| 4758 | xmlBufferCat(buf, string); |
| 4759 | xmlBufferCCat(buf, "'"); |
| 4760 | } else { |
| 4761 | xmlBufferCCat(buf, "\""); |
| 4762 | xmlBufferCat(buf, string); |
| 4763 | xmlBufferCCat(buf, "\""); |
| 4764 | } |
| 4765 | } |
| 4766 | |
| 4767 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 4768 | /************************************************************************ |
| 4769 | * * |
| 4770 | * Dumping XML tree content to a simple buffer * |
| 4771 | * * |
| 4772 | ************************************************************************/ |
| 4773 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4774 | void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4775 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4776 | int format); |
| 4777 | static void |
| 4778 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4779 | int format); |
| 4780 | void |
| 4781 | htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur); |
| 4782 | |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4783 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4784 | * xmlNsDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4785 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4786 | * @cur: a namespace |
| 4787 | * |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4788 | * Dump a local Namespace definition. |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4789 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4790 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4791 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4792 | xmlNsDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4793 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4794 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4795 | xmlGenericError(xmlGenericErrorContext, |
| 4796 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4797 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4798 | return; |
| 4799 | } |
| 4800 | if (cur->type == XML_LOCAL_NAMESPACE) { |
| 4801 | /* Within the context of an element attributes */ |
| 4802 | if (cur->prefix != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4803 | xmlBufferWriteChar(buf, " xmlns:"); |
| 4804 | xmlBufferWriteCHAR(buf, cur->prefix); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4805 | } else |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4806 | xmlBufferWriteChar(buf, " xmlns"); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4807 | xmlBufferWriteChar(buf, "="); |
| 4808 | xmlBufferWriteQuotedString(buf, cur->href); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4809 | } |
| 4810 | } |
| 4811 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4812 | /** |
| 4813 | * xmlNsListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4814 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4815 | * @cur: the first namespace |
| 4816 | * |
| 4817 | * Dump a list of local Namespace definitions. |
| 4818 | * Should be called in the context of attributes dumps. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4819 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4820 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4821 | xmlNsListDump(xmlBufferPtr buf, xmlNsPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4822 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4823 | xmlNsDump(buf, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4824 | cur = cur->next; |
| 4825 | } |
| 4826 | } |
| 4827 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4828 | /** |
| 4829 | * xmlDtdDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4830 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4831 | * @doc: the document |
| 4832 | * |
| 4833 | * Dump the XML document DTD, if any. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4834 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4835 | static void |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4836 | xmlDtdDump(xmlBufferPtr buf, xmlDtdPtr dtd) { |
| 4837 | if (dtd == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4838 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4839 | xmlGenericError(xmlGenericErrorContext, |
| 4840 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4841 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4842 | return; |
| 4843 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4844 | xmlBufferWriteChar(buf, "<!DOCTYPE "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4845 | xmlBufferWriteCHAR(buf, dtd->name); |
| 4846 | if (dtd->ExternalID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4847 | xmlBufferWriteChar(buf, " PUBLIC "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4848 | xmlBufferWriteQuotedString(buf, dtd->ExternalID); |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4849 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4850 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
| 4851 | } else if (dtd->SystemID != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4852 | xmlBufferWriteChar(buf, " SYSTEM "); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4853 | xmlBufferWriteQuotedString(buf, dtd->SystemID); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4854 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4855 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 4856 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 4857 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4858 | return; |
| 4859 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4860 | xmlBufferWriteChar(buf, " [\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4861 | xmlNodeListDump(buf, dtd->doc, dtd->children, -1, 0); |
| 4862 | #if 0 |
| 4863 | if (dtd->entities != NULL) |
| 4864 | xmlDumpEntitiesTable(buf, (xmlEntitiesTablePtr) dtd->entities); |
| 4865 | if (dtd->notations != NULL) |
| 4866 | xmlDumpNotationTable(buf, (xmlNotationTablePtr) dtd->notations); |
| 4867 | if (dtd->elements != NULL) |
| 4868 | xmlDumpElementTable(buf, (xmlElementTablePtr) dtd->elements); |
| 4869 | if (dtd->attributes != NULL) |
| 4870 | xmlDumpAttributeTable(buf, (xmlAttributeTablePtr) dtd->attributes); |
| 4871 | #endif |
| 4872 | xmlBufferWriteChar(buf, "]>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4875 | /** |
| 4876 | * xmlAttrDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4877 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4878 | * @doc: the document |
| 4879 | * @cur: the attribute pointer |
| 4880 | * |
| 4881 | * Dump an XML attribute |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4882 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4883 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4884 | xmlAttrDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4885 | xmlChar *value; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4886 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4887 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4888 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4889 | xmlGenericError(xmlGenericErrorContext, |
| 4890 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4891 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4892 | return; |
| 4893 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4894 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4895 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 4896 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 4897 | xmlBufferWriteChar(buf, ":"); |
| 4898 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4899 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4900 | value = xmlNodeListGetString(doc, cur->children, 0); |
Daniel Veillard | f060a41 | 2001-01-03 20:52:44 +0000 | [diff] [blame] | 4901 | if (value != NULL) { |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4902 | xmlBufferWriteChar(buf, "="); |
| 4903 | xmlBufferWriteQuotedString(buf, value); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4904 | xmlFree(value); |
Daniel Veillard | 726c7e3 | 1999-02-08 15:13:10 +0000 | [diff] [blame] | 4905 | } else { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4906 | xmlBufferWriteChar(buf, "=\"\""); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4910 | /** |
| 4911 | * xmlAttrListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4912 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4913 | * @doc: the document |
| 4914 | * @cur: the first attribute pointer |
| 4915 | * |
| 4916 | * Dump a list of XML attributes |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4917 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4918 | static void |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4919 | xmlAttrListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4920 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4921 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4922 | xmlGenericError(xmlGenericErrorContext, |
| 4923 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4924 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4925 | return; |
| 4926 | } |
| 4927 | while (cur != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 4928 | xmlAttrDump(buf, doc, cur); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4929 | cur = cur->next; |
| 4930 | } |
| 4931 | } |
| 4932 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4933 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 4934 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4935 | /** |
| 4936 | * xmlNodeListDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4937 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4938 | * @doc: the document |
| 4939 | * @cur: the first node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4940 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4941 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4942 | * |
| 4943 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 4944 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 4945 | static void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4946 | xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4947 | int format) { |
| 4948 | int i; |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4949 | |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4950 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4951 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4952 | xmlGenericError(xmlGenericErrorContext, |
| 4953 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4954 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4955 | return; |
| 4956 | } |
| 4957 | while (cur != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4958 | if ((format) && (xmlIndentTreeOutput) && |
| 4959 | (cur->type == XML_ELEMENT_NODE)) |
| 4960 | for (i = 0;i < level;i++) |
| 4961 | xmlBufferWriteChar(buf, " "); |
| 4962 | xmlNodeDump(buf, doc, cur, level, format); |
| 4963 | if (format) { |
| 4964 | xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4965 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4966 | cur = cur->next; |
| 4967 | } |
| 4968 | } |
| 4969 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4970 | /** |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 4971 | * xmlNodeDump: |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 4972 | * @buf: the XML buffer output |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4973 | * @doc: the document |
| 4974 | * @cur: the current node |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4975 | * @level: the imbrication level for indenting |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4976 | * @format: is formatting allowed |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 4977 | * |
| 4978 | * Dump an XML node, recursive behaviour,children are printed too. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4979 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 4980 | void |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4981 | xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, |
| 4982 | int format) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4983 | int i; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 4984 | xmlNodePtr tmp; |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4985 | |
| 4986 | if (cur == NULL) { |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4987 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4988 | xmlGenericError(xmlGenericErrorContext, |
| 4989 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | ad8f99d | 2000-01-15 14:20:03 +0000 | [diff] [blame] | 4990 | #endif |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 4991 | return; |
| 4992 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 4993 | if (cur->type == XML_XINCLUDE_START) |
| 4994 | return; |
| 4995 | if (cur->type == XML_XINCLUDE_END) |
| 4996 | return; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 4997 | if (cur->type == XML_DTD_NODE) { |
| 4998 | xmlDtdDump(buf, (xmlDtdPtr) cur); |
| 4999 | return; |
| 5000 | } |
| 5001 | if (cur->type == XML_ELEMENT_DECL) { |
| 5002 | xmlDumpElementDecl(buf, (xmlElementPtr) cur); |
| 5003 | return; |
| 5004 | } |
| 5005 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5006 | xmlDumpAttributeDecl(buf, (xmlAttributePtr) cur); |
| 5007 | return; |
| 5008 | } |
| 5009 | if (cur->type == XML_ENTITY_DECL) { |
| 5010 | xmlDumpEntityDecl(buf, (xmlEntityPtr) cur); |
| 5011 | return; |
| 5012 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 5013 | if (cur->type == XML_TEXT_NODE) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5014 | if (cur->content != NULL) { |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5015 | if ((cur->name == xmlStringText) || |
| 5016 | (cur->name != xmlStringTextNoenc)) { |
| 5017 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5018 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5019 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5020 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5021 | #else |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5022 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5023 | xmlBufferContent(cur->content)); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5024 | #endif |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5025 | if (buffer != NULL) { |
| 5026 | xmlBufferWriteCHAR(buf, buffer); |
| 5027 | xmlFree(buffer); |
| 5028 | } |
| 5029 | } else { |
| 5030 | /* |
| 5031 | * Disable escaping, needed for XSLT |
| 5032 | */ |
| 5033 | #ifndef XML_USE_BUFFER_CONTENT |
| 5034 | xmlBufferWriteCHAR(buf, cur->content); |
| 5035 | #else |
| 5036 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5037 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5038 | } |
| 5039 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5040 | return; |
| 5041 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5042 | if (cur->type == XML_PI_NODE) { |
| 5043 | if (cur->content != NULL) { |
| 5044 | xmlBufferWriteChar(buf, "<?"); |
| 5045 | xmlBufferWriteCHAR(buf, cur->name); |
| 5046 | if (cur->content != NULL) { |
| 5047 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5048 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5049 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5050 | #else |
| 5051 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5052 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5053 | } |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5054 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5055 | } else { |
| 5056 | xmlBufferWriteChar(buf, "<?"); |
| 5057 | xmlBufferWriteCHAR(buf, cur->name); |
| 5058 | xmlBufferWriteChar(buf, "?>"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5059 | } |
| 5060 | return; |
| 5061 | } |
Daniel Veillard | 0bef131 | 1998-10-14 02:36:47 +0000 | [diff] [blame] | 5062 | if (cur->type == XML_COMMENT_NODE) { |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5063 | if (cur->content != NULL) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5064 | xmlBufferWriteChar(buf, "<!--"); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5065 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5066 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5067 | #else |
| 5068 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5069 | #endif |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5070 | xmlBufferWriteChar(buf, "-->"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5071 | } |
| 5072 | return; |
| 5073 | } |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 5074 | if (cur->type == XML_ENTITY_REF_NODE) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5075 | xmlBufferWriteChar(buf, "&"); |
| 5076 | xmlBufferWriteCHAR(buf, cur->name); |
| 5077 | xmlBufferWriteChar(buf, ";"); |
Daniel Veillard | ccb0963 | 1998-10-27 06:21:04 +0000 | [diff] [blame] | 5078 | return; |
| 5079 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5080 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5081 | xmlBufferWriteChar(buf, "<![CDATA["); |
| 5082 | if (cur->content != NULL) |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5083 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5084 | xmlBufferWriteCHAR(buf, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5085 | #else |
| 5086 | xmlBufferWriteCHAR(buf, xmlBufferContent(cur->content)); |
| 5087 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 5088 | xmlBufferWriteChar(buf, "]]>"); |
| 5089 | return; |
| 5090 | } |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5091 | |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5092 | if (format == 1) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5093 | tmp = cur->children; |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5094 | while (tmp != NULL) { |
| 5095 | if ((tmp->type == XML_TEXT_NODE) || |
| 5096 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5097 | format = 0; |
| 5098 | break; |
| 5099 | } |
| 5100 | tmp = tmp->next; |
| 5101 | } |
| 5102 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5103 | xmlBufferWriteChar(buf, "<"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5104 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5105 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5106 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5109 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5110 | if (cur->nsDef) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5111 | xmlNsListDump(buf, cur->nsDef); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5112 | if (cur->properties != NULL) |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5113 | xmlAttrListDump(buf, doc, cur->properties); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5114 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5115 | if ((cur->content == NULL) && (cur->children == NULL) && |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 5116 | (!xmlSaveNoEmptyTags)) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5117 | xmlBufferWriteChar(buf, "/>"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5118 | return; |
| 5119 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5120 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5121 | if (cur->content != NULL) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5122 | xmlChar *buffer; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5123 | |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5124 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5125 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
Daniel Veillard | f5c2c87 | 1999-12-01 09:51:45 +0000 | [diff] [blame] | 5126 | #else |
| 5127 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5128 | xmlBufferContent(cur->content)); |
| 5129 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5130 | if (buffer != NULL) { |
| 5131 | xmlBufferWriteCHAR(buf, buffer); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 5132 | xmlFree(buffer); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 5133 | } |
| 5134 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5135 | if (cur->children != NULL) { |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5136 | if (format) xmlBufferWriteChar(buf, "\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 5137 | xmlNodeListDump(buf, doc, cur->children, |
Daniel Veillard | 3e6d237 | 2000-03-04 11:39:43 +0000 | [diff] [blame] | 5138 | (level >= 0?level+1:-1), format); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5139 | if ((xmlIndentTreeOutput) && (format)) |
| 5140 | for (i = 0;i < level;i++) |
| 5141 | xmlBufferWriteChar(buf, " "); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5142 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5143 | xmlBufferWriteChar(buf, "</"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5144 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5145 | xmlBufferWriteCHAR(buf, cur->ns->prefix); |
| 5146 | xmlBufferWriteChar(buf, ":"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5147 | } |
| 5148 | |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5149 | xmlBufferWriteCHAR(buf, cur->name); |
Daniel Veillard | 7d2c276 | 1999-10-11 15:09:51 +0000 | [diff] [blame] | 5150 | xmlBufferWriteChar(buf, ">"); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5153 | /** |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5154 | * xmlElemDump: |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 5155 | * @f: the FILE * for the output |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5156 | * @doc: the document |
| 5157 | * @cur: the current node |
| 5158 | * |
| 5159 | * Dump an XML/HTML node, recursive behaviour,children are printed too. |
| 5160 | */ |
| 5161 | void |
| 5162 | xmlElemDump(FILE *f, xmlDocPtr doc, xmlNodePtr cur) { |
| 5163 | xmlBufferPtr buf; |
| 5164 | |
| 5165 | if (cur == NULL) { |
| 5166 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5167 | xmlGenericError(xmlGenericErrorContext, |
| 5168 | "xmlElemDump : cur == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5169 | #endif |
| 5170 | return; |
| 5171 | } |
| 5172 | if (doc == NULL) { |
| 5173 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5174 | xmlGenericError(xmlGenericErrorContext, |
| 5175 | "xmlElemDump : doc == NULL\n"); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5176 | #endif |
| 5177 | } |
| 5178 | buf = xmlBufferCreate(); |
| 5179 | if (buf == NULL) return; |
| 5180 | if ((doc != NULL) && |
| 5181 | (doc->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5182 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5183 | htmlNodeDump(buf, doc, cur); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5184 | #else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5185 | xmlGenericError(xmlGenericErrorContext, |
| 5186 | "HTML support not compiled in\n"); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 5187 | #endif /* LIBXML_HTML_ENABLED */ |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5188 | } else |
| 5189 | xmlNodeDump(buf, doc, cur, 0, 1); |
| 5190 | xmlBufferDump(f, buf); |
| 5191 | xmlBufferFree(buf); |
| 5192 | } |
| 5193 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5194 | /************************************************************************ |
| 5195 | * * |
| 5196 | * Dumping XML tree content to an I/O output buffer * |
| 5197 | * * |
| 5198 | ************************************************************************/ |
| 5199 | |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5200 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5201 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5202 | int level, int format, const char *encoding); |
| 5203 | static void |
| 5204 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5205 | int level, int format, const char *encoding); |
| 5206 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5207 | * xmlNsDumpOutput: |
| 5208 | * @buf: the XML buffer output |
| 5209 | * @cur: a namespace |
| 5210 | * |
| 5211 | * Dump a local Namespace definition. |
| 5212 | * Should be called in the context of attributes dumps. |
| 5213 | */ |
| 5214 | static void |
| 5215 | xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5216 | if (cur == NULL) { |
| 5217 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5218 | xmlGenericError(xmlGenericErrorContext, |
| 5219 | "xmlNsDump : Ns == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5220 | #endif |
| 5221 | return; |
| 5222 | } |
Daniel Veillard | e0854c3 | 2000-08-27 21:12:29 +0000 | [diff] [blame] | 5223 | if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5224 | /* Within the context of an element attributes */ |
| 5225 | if (cur->prefix != NULL) { |
| 5226 | xmlOutputBufferWriteString(buf, " xmlns:"); |
| 5227 | xmlOutputBufferWriteString(buf, (const char *)cur->prefix); |
| 5228 | } else |
| 5229 | xmlOutputBufferWriteString(buf, " xmlns"); |
| 5230 | xmlOutputBufferWriteString(buf, "="); |
| 5231 | xmlBufferWriteQuotedString(buf->buffer, cur->href); |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | /** |
| 5236 | * xmlNsListDumpOutput: |
| 5237 | * @buf: the XML buffer output |
| 5238 | * @cur: the first namespace |
| 5239 | * |
| 5240 | * Dump a list of local Namespace definitions. |
| 5241 | * Should be called in the context of attributes dumps. |
| 5242 | */ |
| 5243 | static void |
| 5244 | xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) { |
| 5245 | while (cur != NULL) { |
| 5246 | xmlNsDumpOutput(buf, cur); |
| 5247 | cur = cur->next; |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | /** |
| 5252 | * xmlDtdDumpOutput: |
| 5253 | * @buf: the XML buffer output |
| 5254 | * @doc: the document |
| 5255 | * @encoding: an optional encoding string |
| 5256 | * |
| 5257 | * Dump the XML document DTD, if any. |
| 5258 | */ |
| 5259 | static void |
| 5260 | xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) { |
| 5261 | if (dtd == NULL) { |
| 5262 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5263 | xmlGenericError(xmlGenericErrorContext, |
| 5264 | "xmlDtdDump : no internal subset\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5265 | #endif |
| 5266 | return; |
| 5267 | } |
| 5268 | xmlOutputBufferWriteString(buf, "<!DOCTYPE "); |
| 5269 | xmlOutputBufferWriteString(buf, (const char *)dtd->name); |
| 5270 | if (dtd->ExternalID != NULL) { |
| 5271 | xmlOutputBufferWriteString(buf, " PUBLIC "); |
| 5272 | xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); |
| 5273 | xmlOutputBufferWriteString(buf, " "); |
| 5274 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5275 | } else if (dtd->SystemID != NULL) { |
| 5276 | xmlOutputBufferWriteString(buf, " SYSTEM "); |
| 5277 | xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); |
| 5278 | } |
| 5279 | if ((dtd->entities == NULL) && (dtd->elements == NULL) && |
| 5280 | (dtd->attributes == NULL) && (dtd->notations == NULL)) { |
| 5281 | xmlOutputBufferWriteString(buf, ">"); |
| 5282 | return; |
| 5283 | } |
| 5284 | xmlOutputBufferWriteString(buf, " [\n"); |
| 5285 | xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5286 | xmlOutputBufferWriteString(buf, "]>"); |
| 5287 | } |
| 5288 | |
| 5289 | /** |
| 5290 | * xmlAttrDumpOutput: |
| 5291 | * @buf: the XML buffer output |
| 5292 | * @doc: the document |
| 5293 | * @cur: the attribute pointer |
| 5294 | * @encoding: an optional encoding string |
| 5295 | * |
| 5296 | * Dump an XML attribute |
| 5297 | */ |
| 5298 | static void |
| 5299 | xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur, |
| 5300 | const char *encoding) { |
| 5301 | xmlChar *value; |
| 5302 | |
| 5303 | if (cur == NULL) { |
| 5304 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5305 | xmlGenericError(xmlGenericErrorContext, |
| 5306 | "xmlAttrDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5307 | #endif |
| 5308 | return; |
| 5309 | } |
| 5310 | xmlOutputBufferWriteString(buf, " "); |
| 5311 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5312 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5313 | xmlOutputBufferWriteString(buf, ":"); |
| 5314 | } |
| 5315 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5316 | value = xmlNodeListGetString(doc, cur->children, 0); |
| 5317 | if (value) { |
| 5318 | xmlOutputBufferWriteString(buf, "="); |
| 5319 | xmlBufferWriteQuotedString(buf->buffer, value); |
| 5320 | xmlFree(value); |
| 5321 | } else { |
| 5322 | xmlOutputBufferWriteString(buf, "=\"\""); |
| 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | /** |
| 5327 | * xmlAttrListDumpOutput: |
| 5328 | * @buf: the XML buffer output |
| 5329 | * @doc: the document |
| 5330 | * @cur: the first attribute pointer |
| 5331 | * @encoding: an optional encoding string |
| 5332 | * |
| 5333 | * Dump a list of XML attributes |
| 5334 | */ |
| 5335 | static void |
| 5336 | xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5337 | xmlAttrPtr cur, const char *encoding) { |
| 5338 | if (cur == NULL) { |
| 5339 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5340 | xmlGenericError(xmlGenericErrorContext, |
| 5341 | "xmlAttrListDump : property == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5342 | #endif |
| 5343 | return; |
| 5344 | } |
| 5345 | while (cur != NULL) { |
| 5346 | xmlAttrDumpOutput(buf, doc, cur, encoding); |
| 5347 | cur = cur->next; |
| 5348 | } |
| 5349 | } |
| 5350 | |
| 5351 | |
| 5352 | |
| 5353 | /** |
| 5354 | * xmlNodeListDumpOutput: |
| 5355 | * @buf: the XML buffer output |
| 5356 | * @doc: the document |
| 5357 | * @cur: the first node |
| 5358 | * @level: the imbrication level for indenting |
| 5359 | * @format: is formatting allowed |
| 5360 | * @encoding: an optional encoding string |
| 5361 | * |
| 5362 | * Dump an XML node list, recursive behaviour,children are printed too. |
| 5363 | */ |
| 5364 | static void |
| 5365 | xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, |
| 5366 | xmlNodePtr cur, int level, int format, const char *encoding) { |
| 5367 | int i; |
| 5368 | |
| 5369 | if (cur == NULL) { |
| 5370 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5371 | xmlGenericError(xmlGenericErrorContext, |
| 5372 | "xmlNodeListDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5373 | #endif |
| 5374 | return; |
| 5375 | } |
| 5376 | while (cur != NULL) { |
| 5377 | if ((format) && (xmlIndentTreeOutput) && |
| 5378 | (cur->type == XML_ELEMENT_NODE)) |
| 5379 | for (i = 0;i < level;i++) |
| 5380 | xmlOutputBufferWriteString(buf, " "); |
| 5381 | xmlNodeDumpOutput(buf, doc, cur, level, format, encoding); |
| 5382 | if (format) { |
| 5383 | xmlOutputBufferWriteString(buf, "\n"); |
| 5384 | } |
| 5385 | cur = cur->next; |
| 5386 | } |
| 5387 | } |
| 5388 | |
| 5389 | /** |
| 5390 | * xmlNodeDumpOutput: |
| 5391 | * @buf: the XML buffer output |
| 5392 | * @doc: the document |
| 5393 | * @cur: the current node |
| 5394 | * @level: the imbrication level for indenting |
| 5395 | * @format: is formatting allowed |
| 5396 | * @encoding: an optional encoding string |
| 5397 | * |
| 5398 | * Dump an XML node, recursive behaviour,children are printed too. |
| 5399 | */ |
Daniel Veillard | b656ebe | 2000-09-22 13:51:48 +0000 | [diff] [blame] | 5400 | void |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5401 | xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, |
| 5402 | int level, int format, const char *encoding) { |
| 5403 | int i; |
| 5404 | xmlNodePtr tmp; |
| 5405 | |
| 5406 | if (cur == NULL) { |
| 5407 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5408 | xmlGenericError(xmlGenericErrorContext, |
| 5409 | "xmlNodeDump : node == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5410 | #endif |
| 5411 | return; |
| 5412 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 5413 | if (cur->type == XML_XINCLUDE_START) |
| 5414 | return; |
| 5415 | if (cur->type == XML_XINCLUDE_END) |
| 5416 | return; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5417 | if (cur->type == XML_DTD_NODE) { |
| 5418 | xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding); |
| 5419 | return; |
| 5420 | } |
| 5421 | if (cur->type == XML_ELEMENT_DECL) { |
| 5422 | xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); |
| 5423 | return; |
| 5424 | } |
| 5425 | if (cur->type == XML_ATTRIBUTE_DECL) { |
| 5426 | xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); |
| 5427 | return; |
| 5428 | } |
| 5429 | if (cur->type == XML_ENTITY_DECL) { |
| 5430 | xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); |
| 5431 | return; |
| 5432 | } |
| 5433 | if (cur->type == XML_TEXT_NODE) { |
| 5434 | if (cur->content != NULL) { |
Daniel Veillard | cb126ac | 2001-01-18 12:50:20 +0000 | [diff] [blame] | 5435 | if ((cur->name == xmlStringText) || |
| 5436 | (cur->name != xmlStringTextNoenc)) { |
| 5437 | xmlChar *buffer; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5438 | |
| 5439 | #ifndef XML_USE_BUFFER_CONTENT |
Daniel Veillard | cb126ac | 2001-01-18 12:50:20 +0000 | [diff] [blame] | 5440 | if (encoding == NULL) |
| 5441 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5442 | else |
| 5443 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5444 | #else |
Daniel Veillard | cb126ac | 2001-01-18 12:50:20 +0000 | [diff] [blame] | 5445 | if (encoding == NULL) |
| 5446 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5447 | xmlBufferContent(cur->content)); |
| 5448 | else |
| 5449 | buffer = xmlEncodeSpecialChars(doc, |
| 5450 | xmlBufferContent(cur->content)); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5451 | #endif |
Daniel Veillard | cb126ac | 2001-01-18 12:50:20 +0000 | [diff] [blame] | 5452 | if (buffer != NULL) { |
| 5453 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5454 | xmlFree(buffer); |
| 5455 | } |
| 5456 | } else { |
| 5457 | /* |
| 5458 | * Disable escaping, needed for XSLT |
| 5459 | */ |
| 5460 | #ifndef XML_USE_BUFFER_CONTENT |
| 5461 | xmlOutputBufferWriteString(buf, cur->content); |
| 5462 | #else |
| 5463 | xmlOutputBufferWriteString(buf, xmlBufferContent(cur->content)); |
| 5464 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
Daniel Veillard | cb126ac | 2001-01-18 12:50:20 +0000 | [diff] [blame] | 5467 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5468 | return; |
| 5469 | } |
| 5470 | if (cur->type == XML_PI_NODE) { |
| 5471 | if (cur->content != NULL) { |
| 5472 | xmlOutputBufferWriteString(buf, "<?"); |
| 5473 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5474 | if (cur->content != NULL) { |
| 5475 | xmlOutputBufferWriteString(buf, " "); |
| 5476 | #ifndef XML_USE_BUFFER_CONTENT |
| 5477 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5478 | #else |
| 5479 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5480 | #endif |
| 5481 | } |
| 5482 | xmlOutputBufferWriteString(buf, "?>"); |
| 5483 | } else { |
| 5484 | xmlOutputBufferWriteString(buf, "<?"); |
| 5485 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5486 | xmlOutputBufferWriteString(buf, "?>"); |
| 5487 | } |
| 5488 | return; |
| 5489 | } |
| 5490 | if (cur->type == XML_COMMENT_NODE) { |
| 5491 | if (cur->content != NULL) { |
| 5492 | xmlOutputBufferWriteString(buf, "<!--"); |
| 5493 | #ifndef XML_USE_BUFFER_CONTENT |
| 5494 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5495 | #else |
| 5496 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5497 | #endif |
| 5498 | xmlOutputBufferWriteString(buf, "-->"); |
| 5499 | } |
| 5500 | return; |
| 5501 | } |
| 5502 | if (cur->type == XML_ENTITY_REF_NODE) { |
| 5503 | xmlOutputBufferWriteString(buf, "&"); |
| 5504 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5505 | xmlOutputBufferWriteString(buf, ";"); |
| 5506 | return; |
| 5507 | } |
| 5508 | if (cur->type == XML_CDATA_SECTION_NODE) { |
| 5509 | xmlOutputBufferWriteString(buf, "<![CDATA["); |
| 5510 | if (cur->content != NULL) |
| 5511 | #ifndef XML_USE_BUFFER_CONTENT |
| 5512 | xmlOutputBufferWriteString(buf, (const char *)cur->content); |
| 5513 | #else |
| 5514 | xmlOutputBufferWriteString(buf, (const char *)xmlBufferContent(cur->content)); |
| 5515 | #endif |
| 5516 | xmlOutputBufferWriteString(buf, "]]>"); |
| 5517 | return; |
| 5518 | } |
| 5519 | |
| 5520 | if (format == 1) { |
| 5521 | tmp = cur->children; |
| 5522 | while (tmp != NULL) { |
| 5523 | if ((tmp->type == XML_TEXT_NODE) || |
| 5524 | (tmp->type == XML_ENTITY_REF_NODE)) { |
| 5525 | format = 0; |
| 5526 | break; |
| 5527 | } |
| 5528 | tmp = tmp->next; |
| 5529 | } |
| 5530 | } |
| 5531 | xmlOutputBufferWriteString(buf, "<"); |
| 5532 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5533 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5534 | xmlOutputBufferWriteString(buf, ":"); |
| 5535 | } |
| 5536 | |
| 5537 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5538 | if (cur->nsDef) |
| 5539 | xmlNsListDumpOutput(buf, cur->nsDef); |
| 5540 | if (cur->properties != NULL) |
| 5541 | xmlAttrListDumpOutput(buf, doc, cur->properties, encoding); |
| 5542 | |
| 5543 | if ((cur->content == NULL) && (cur->children == NULL) && |
| 5544 | (!xmlSaveNoEmptyTags)) { |
| 5545 | xmlOutputBufferWriteString(buf, "/>"); |
| 5546 | return; |
| 5547 | } |
| 5548 | xmlOutputBufferWriteString(buf, ">"); |
| 5549 | if (cur->content != NULL) { |
| 5550 | xmlChar *buffer; |
| 5551 | |
| 5552 | #ifndef XML_USE_BUFFER_CONTENT |
| 5553 | if (encoding == NULL) |
| 5554 | buffer = xmlEncodeEntitiesReentrant(doc, cur->content); |
| 5555 | else |
| 5556 | buffer = xmlEncodeSpecialChars(doc, cur->content); |
| 5557 | #else |
| 5558 | if (encoding == NULL) |
| 5559 | buffer = xmlEncodeEntitiesReentrant(doc, |
| 5560 | xmlBufferContent(cur->content)); |
| 5561 | else |
| 5562 | buffer = xmlEncodeSpecialChars(doc, |
| 5563 | xmlBufferContent(cur->content)); |
| 5564 | #endif |
| 5565 | if (buffer != NULL) { |
| 5566 | xmlOutputBufferWriteString(buf, (const char *)buffer); |
| 5567 | xmlFree(buffer); |
| 5568 | } |
| 5569 | } |
| 5570 | if (cur->children != NULL) { |
| 5571 | if (format) xmlOutputBufferWriteString(buf, "\n"); |
| 5572 | xmlNodeListDumpOutput(buf, doc, cur->children, |
| 5573 | (level >= 0?level+1:-1), format, encoding); |
| 5574 | if ((xmlIndentTreeOutput) && (format)) |
| 5575 | for (i = 0;i < level;i++) |
| 5576 | xmlOutputBufferWriteString(buf, " "); |
| 5577 | } |
| 5578 | xmlOutputBufferWriteString(buf, "</"); |
| 5579 | if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) { |
| 5580 | xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix); |
| 5581 | xmlOutputBufferWriteString(buf, ":"); |
| 5582 | } |
| 5583 | |
| 5584 | xmlOutputBufferWriteString(buf, (const char *)cur->name); |
| 5585 | xmlOutputBufferWriteString(buf, ">"); |
| 5586 | } |
| 5587 | |
| 5588 | /** |
| 5589 | * xmlDocContentDumpOutput: |
| 5590 | * @buf: the XML buffer output |
| 5591 | * @cur: the document |
| 5592 | * @encoding: an optional encoding string |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5593 | * @format: should formatting spaces been added |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5594 | * |
| 5595 | * Dump an XML document. |
| 5596 | */ |
| 5597 | static void |
| 5598 | xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur, |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5599 | const char *encoding, int format) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5600 | xmlOutputBufferWriteString(buf, "<?xml version="); |
| 5601 | if (cur->version != NULL) |
| 5602 | xmlBufferWriteQuotedString(buf->buffer, cur->version); |
| 5603 | else |
| 5604 | xmlOutputBufferWriteString(buf, "\"1.0\""); |
| 5605 | if (encoding == NULL) { |
| 5606 | if (cur->encoding != NULL) |
| 5607 | encoding = (const char *) cur->encoding; |
| 5608 | else if (cur->charset != XML_CHAR_ENCODING_UTF8) |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5609 | encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5610 | } |
| 5611 | if (encoding != NULL) { |
| 5612 | xmlOutputBufferWriteString(buf, " encoding="); |
| 5613 | xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); |
| 5614 | } |
| 5615 | switch (cur->standalone) { |
| 5616 | case 0: |
| 5617 | xmlOutputBufferWriteString(buf, " standalone=\"no\""); |
| 5618 | break; |
| 5619 | case 1: |
| 5620 | xmlOutputBufferWriteString(buf, " standalone=\"yes\""); |
| 5621 | break; |
| 5622 | } |
| 5623 | xmlOutputBufferWriteString(buf, "?>\n"); |
| 5624 | if (cur->children != NULL) { |
| 5625 | xmlNodePtr child = cur->children; |
| 5626 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5627 | while (child != NULL) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5628 | xmlNodeDumpOutput(buf, cur, child, 0, format, encoding); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5629 | xmlOutputBufferWriteString(buf, "\n"); |
| 5630 | child = child->next; |
| 5631 | } |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | /************************************************************************ |
| 5636 | * * |
| 5637 | * Saving functions front-ends * |
| 5638 | * * |
| 5639 | ************************************************************************/ |
| 5640 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5641 | /** |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5642 | * xmlDocDumpMemoryEnc: |
| 5643 | * @out_doc: Document to generate XML text from |
| 5644 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 5645 | * @doc_txt_len: Length of the generated XML text |
| 5646 | * @txt_encoding: Character encoding to use when generating XML text |
| 5647 | * @format: should formatting spaces been added |
| 5648 | * |
| 5649 | * Dump the current DOM tree into memory using the character encoding specified |
| 5650 | * by the caller. Note it is up to the caller of this function to free the |
| 5651 | * allocated memory. |
| 5652 | */ |
| 5653 | |
| 5654 | void |
| 5655 | xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 5656 | int * doc_txt_len, const char * txt_encoding, int format) { |
| 5657 | int dummy = 0; |
| 5658 | |
| 5659 | xmlCharEncoding doc_charset; |
| 5660 | xmlOutputBufferPtr out_buff = NULL; |
| 5661 | xmlCharEncodingHandlerPtr conv_hdlr = NULL; |
| 5662 | |
| 5663 | if (doc_txt_len == NULL) { |
| 5664 | doc_txt_len = &dummy; /* Continue, caller just won't get length */ |
| 5665 | } |
| 5666 | |
| 5667 | if (doc_txt_ptr == NULL) { |
| 5668 | *doc_txt_len = 0; |
| 5669 | xmlGenericError(xmlGenericErrorContext, |
| 5670 | "xmlDocDumpFormatMemoryEnc: Null return buffer pointer."); |
| 5671 | return; |
| 5672 | } |
| 5673 | |
| 5674 | *doc_txt_ptr = NULL; |
| 5675 | *doc_txt_len = 0; |
| 5676 | |
| 5677 | if (out_doc == NULL) { |
| 5678 | /* No document, no output */ |
| 5679 | xmlGenericError(xmlGenericErrorContext, |
| 5680 | "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n"); |
| 5681 | return; |
| 5682 | } |
| 5683 | |
| 5684 | /* |
| 5685 | * Validate the encoding value, if provided. |
| 5686 | * This logic is copied from xmlSaveFileEnc. |
| 5687 | */ |
| 5688 | |
| 5689 | if (txt_encoding == NULL) |
| 5690 | txt_encoding = (const char *) out_doc->encoding; |
| 5691 | if (txt_encoding != NULL) { |
| 5692 | doc_charset = xmlParseCharEncoding(txt_encoding); |
| 5693 | |
| 5694 | if (out_doc->charset != XML_CHAR_ENCODING_UTF8) { |
| 5695 | xmlGenericError(xmlGenericErrorContext, |
| 5696 | "xmlDocDumpFormatMemoryEnc: Source document not in UTF8\n"); |
| 5697 | return; |
| 5698 | |
| 5699 | } else if (doc_charset != XML_CHAR_ENCODING_UTF8) { |
| 5700 | conv_hdlr = xmlFindCharEncodingHandler(txt_encoding); |
| 5701 | if ( conv_hdlr == NULL ) { |
| 5702 | xmlGenericError(xmlGenericErrorContext, |
| 5703 | "%s: %s %s '%s'\n", |
| 5704 | "xmlDocDumpFormatMemoryEnc", |
| 5705 | "Failed to identify encoding handler for", |
| 5706 | "character set", |
| 5707 | txt_encoding); |
| 5708 | return; |
| 5709 | } |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) { |
| 5714 | xmlGenericError(xmlGenericErrorContext, |
| 5715 | "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n"); |
| 5716 | return; |
| 5717 | } |
| 5718 | |
| 5719 | xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, 1); |
| 5720 | xmlOutputBufferFlush(out_buff); |
| 5721 | if (out_buff->conv != NULL) { |
| 5722 | *doc_txt_len = out_buff->conv->use; |
| 5723 | *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); |
| 5724 | } else { |
| 5725 | *doc_txt_len = out_buff->buffer->use; |
| 5726 | *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); |
| 5727 | } |
| 5728 | (void)xmlOutputBufferClose(out_buff); |
| 5729 | |
| 5730 | if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) { |
| 5731 | *doc_txt_len = 0; |
| 5732 | xmlGenericError(xmlGenericErrorContext, |
| 5733 | "xmlDocDumpFormatMemoryEnc: %s\n", |
| 5734 | "Failed to allocate memory for document text representation."); |
| 5735 | } |
| 5736 | |
| 5737 | return; |
| 5738 | } |
| 5739 | |
| 5740 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5741 | * xmlDocDumpMemory: |
| 5742 | * @cur: the document |
| 5743 | * @mem: OUT: the memory pointer |
| 5744 | * @size: OUT: the memory lenght |
| 5745 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5746 | * 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] | 5747 | * It's up to the caller to free the memory. |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5748 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5749 | void |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5750 | xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5751 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0); |
| 5752 | } |
Daniel Veillard | 5099ae8 | 1999-04-21 20:12:07 +0000 | [diff] [blame] | 5753 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5754 | /** |
| 5755 | * xmlDocDumpFormatMemory: |
| 5756 | * @cur: the document |
| 5757 | * @mem: OUT: the memory pointer |
| 5758 | * @size: OUT: the memory lenght |
| 5759 | * @format: should formatting spaces been added |
| 5760 | * |
| 5761 | * |
| 5762 | * Dump an XML document in memory and return the xmlChar * and it's size. |
| 5763 | * It's up to the caller to free the memory. |
| 5764 | */ |
| 5765 | void |
| 5766 | xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) { |
| 5767 | xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format); |
Daniel Veillard | 260a68f | 1998-08-13 03:39:55 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5770 | /** |
Daniel Veillard | 58770e7 | 2000-11-25 00:48:47 +0000 | [diff] [blame] | 5771 | * xmlDocDumpMemoryEnc: |
| 5772 | * @out_doc: Document to generate XML text from |
| 5773 | * @doc_txt_ptr: Memory pointer for allocated XML text |
| 5774 | * @doc_txt_len: Length of the generated XML text |
| 5775 | * @txt_encoding: Character encoding to use when generating XML text |
| 5776 | * |
| 5777 | * Dump the current DOM tree into memory using the character encoding specified |
| 5778 | * by the caller. Note it is up to the caller of this function to free the |
| 5779 | * allocated memory. |
| 5780 | */ |
| 5781 | |
| 5782 | void |
| 5783 | xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr, |
| 5784 | int * doc_txt_len, const char * txt_encoding) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5785 | xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len, |
| 5786 | txt_encoding, 1); |
Daniel Veillard | 58770e7 | 2000-11-25 00:48:47 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5790 | * xmlGetDocCompressMode: |
| 5791 | * @doc: the document |
| 5792 | * |
| 5793 | * get the compression ratio for a document, ZLIB based |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5794 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5795 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5796 | int |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 5797 | xmlGetDocCompressMode (xmlDocPtr doc) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5798 | if (doc == NULL) return(-1); |
| 5799 | return(doc->compression); |
| 5800 | } |
| 5801 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5802 | /** |
| 5803 | * xmlSetDocCompressMode: |
| 5804 | * @doc: the document |
| 5805 | * @mode: the compression ratio |
| 5806 | * |
| 5807 | * set the compression ratio for a document, ZLIB based |
| 5808 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5809 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5810 | void |
| 5811 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5812 | if (doc == NULL) return; |
| 5813 | if (mode < 0) doc->compression = 0; |
| 5814 | else if (mode > 9) doc->compression = 9; |
| 5815 | else doc->compression = mode; |
| 5816 | } |
| 5817 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5818 | /** |
| 5819 | * xmlGetCompressMode: |
| 5820 | * |
| 5821 | * get the default compression mode used, ZLIB based. |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 5822 | * Returns 0 (uncompressed) to 9 (max compression) |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5823 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5824 | int |
| 5825 | xmlGetCompressMode(void) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5826 | return(xmlCompressMode); |
| 5827 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 5828 | |
| 5829 | /** |
| 5830 | * xmlSetCompressMode: |
| 5831 | * @mode: the compression ratio |
| 5832 | * |
| 5833 | * set the default compression mode used, ZLIB based |
| 5834 | * Correct values: 0 (uncompressed) to 9 (max compression) |
| 5835 | */ |
Daniel Veillard | baf4cd5 | 1998-10-27 22:56:57 +0000 | [diff] [blame] | 5836 | void |
| 5837 | xmlSetCompressMode(int mode) { |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5838 | if (mode < 0) xmlCompressMode = 0; |
Daniel Veillard | 15a8df4 | 1998-09-24 19:15:06 +0000 | [diff] [blame] | 5839 | else if (mode > 9) xmlCompressMode = 9; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5840 | else xmlCompressMode = mode; |
| 5841 | } |
| 5842 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5843 | /** |
| 5844 | * xmlDocDump: |
| 5845 | * @f: the FILE* |
| 5846 | * @cur: the document |
| 5847 | * |
| 5848 | * Dump an XML document to an open FILE. |
| 5849 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5850 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5851 | */ |
| 5852 | int |
| 5853 | xmlDocDump(FILE *f, xmlDocPtr cur) { |
| 5854 | xmlOutputBufferPtr buf; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5855 | const char * encoding; |
| 5856 | xmlCharEncodingHandlerPtr handler = NULL; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5857 | int ret; |
Daniel Veillard | 151b1b0 | 1998-09-23 00:49:46 +0000 | [diff] [blame] | 5858 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5859 | if (cur == NULL) { |
| 5860 | #ifdef DEBUG_TREE |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5861 | xmlGenericError(xmlGenericErrorContext, |
| 5862 | "xmlDocDump : document == NULL\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5863 | #endif |
| 5864 | return(-1); |
| 5865 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5866 | encoding = (const char *) cur->encoding; |
| 5867 | |
| 5868 | if (encoding != NULL) { |
| 5869 | xmlCharEncoding enc; |
| 5870 | |
| 5871 | enc = xmlParseCharEncoding(encoding); |
| 5872 | |
| 5873 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5874 | xmlGenericError(xmlGenericErrorContext, |
| 5875 | "xmlDocDump: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5876 | return(-1); |
| 5877 | } |
| 5878 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5879 | handler = xmlFindCharEncodingHandler(encoding); |
| 5880 | if (handler == NULL) { |
| 5881 | xmlFree((char *) cur->encoding); |
| 5882 | cur->encoding = NULL; |
| 5883 | } |
| 5884 | } |
| 5885 | } |
| 5886 | buf = xmlOutputBufferCreateFile(f, handler); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5887 | if (buf == NULL) return(-1); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5888 | xmlDocContentDumpOutput(buf, cur, NULL, 1); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5889 | |
| 5890 | ret = xmlOutputBufferClose(buf); |
| 5891 | return(ret); |
| 5892 | } |
| 5893 | |
| 5894 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5895 | * xmlSaveFileTo: |
| 5896 | * @buf: an output I/O buffer |
| 5897 | * @cur: the document |
| 5898 | * @encoding: the encoding if any assuming the i/O layer handles the trancoding |
| 5899 | * |
| 5900 | * Dump an XML document to an I/O buffer. |
| 5901 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5902 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5903 | */ |
| 5904 | int |
| 5905 | xmlSaveFileTo(xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding) { |
| 5906 | int ret; |
| 5907 | |
| 5908 | if (buf == NULL) return(0); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5909 | xmlDocContentDumpOutput(buf, cur, encoding, 1); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5910 | ret = xmlOutputBufferClose(buf); |
| 5911 | return(ret); |
| 5912 | } |
| 5913 | |
| 5914 | /** |
| 5915 | * xmlSaveFileEnc: |
| 5916 | * @filename: the filename (or URL) |
| 5917 | * @cur: the document |
| 5918 | * @encoding: the name of an encoding (or NULL) |
| 5919 | * |
| 5920 | * Dump an XML document, converting it to the given encoding |
| 5921 | * |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5922 | * returns: the number of byte written or -1 in case of failure. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5923 | */ |
| 5924 | int |
| 5925 | xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) { |
| 5926 | xmlOutputBufferPtr buf; |
| 5927 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5928 | int ret; |
| 5929 | |
| 5930 | if (encoding != NULL) { |
| 5931 | xmlCharEncoding enc; |
| 5932 | |
| 5933 | enc = xmlParseCharEncoding(encoding); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5934 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5935 | xmlGenericError(xmlGenericErrorContext, |
| 5936 | "xmlSaveFileEnc: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5937 | return(-1); |
| 5938 | } |
| 5939 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5940 | handler = xmlFindCharEncodingHandler(encoding); |
| 5941 | if (handler == NULL) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5942 | return(-1); |
| 5943 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5944 | } |
| 5945 | } |
| 5946 | |
| 5947 | /* |
| 5948 | * save the content to a temp buffer. |
| 5949 | */ |
| 5950 | buf = xmlOutputBufferCreateFilename(filename, handler, 0); |
Daniel Veillard | f831bfb | 2001-01-16 17:26:04 +0000 | [diff] [blame] | 5951 | if (buf == NULL) return(-1); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5952 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5953 | xmlDocContentDumpOutput(buf, cur, encoding, 1); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5954 | |
| 5955 | ret = xmlOutputBufferClose(buf); |
| 5956 | return(ret); |
| 5957 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5958 | |
| 5959 | /** |
| 5960 | * xmlSaveFile: |
| 5961 | * @filename: the filename (or URL) |
| 5962 | * @cur: the document |
| 5963 | * |
| 5964 | * Dump an XML document to a file. Will use compression if |
| 5965 | * compiled in and enabled. If @filename is "-" the stdout file is |
| 5966 | * used. |
| 5967 | * returns: the number of byte written or -1 in case of failure. |
| 5968 | */ |
| 5969 | int |
| 5970 | xmlSaveFile(const char *filename, xmlDocPtr cur) { |
| 5971 | xmlOutputBufferPtr buf; |
| 5972 | const char *encoding; |
| 5973 | xmlCharEncodingHandlerPtr handler = NULL; |
| 5974 | int ret; |
| 5975 | |
| 5976 | if (cur == NULL) |
| 5977 | return(-1); |
| 5978 | encoding = (const char *) cur->encoding; |
| 5979 | |
| 5980 | /* |
| 5981 | * save the content to a temp buffer. |
| 5982 | */ |
| 5983 | #ifdef HAVE_ZLIB_H |
| 5984 | if (cur->compression < 0) cur->compression = xmlCompressMode; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5985 | #endif |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5986 | if (encoding != NULL) { |
| 5987 | xmlCharEncoding enc; |
| 5988 | |
| 5989 | enc = xmlParseCharEncoding(encoding); |
| 5990 | |
| 5991 | if (cur->charset != XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5992 | xmlGenericError(xmlGenericErrorContext, |
| 5993 | "xmlSaveFile: document not in UTF8\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 5994 | return(-1); |
| 5995 | } |
| 5996 | if (enc != XML_CHAR_ENCODING_UTF8) { |
| 5997 | handler = xmlFindCharEncodingHandler(encoding); |
| 5998 | if (handler == NULL) { |
| 5999 | xmlFree((char *) cur->encoding); |
| 6000 | cur->encoding = NULL; |
| 6001 | } |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression); |
Daniel Veillard | 0cede08 | 2001-01-17 08:23:04 +0000 | [diff] [blame] | 6006 | if (buf == NULL) return(-1); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 6007 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 6008 | xmlDocContentDumpOutput(buf, cur, NULL, 1); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 6009 | |
| 6010 | ret = xmlOutputBufferClose(buf); |
| 6011 | return(ret); |
| 6012 | } |
| 6013 | |