blob: e9d50e76efc5ecb38fb6efc2e6423f4fe22a9504 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardd1640922001-12-17 15:30:10 +00002 * tree.c : implementation of access function for an XML tree.
Owen Taylor3473f882001-02-23 17:55:21 +00003 *
Daniel Veillardd5c2f922002-11-21 14:10:52 +00004 * References:
5 * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/
6 *
Owen Taylor3473f882001-02-23 17:55:21 +00007 * See Copyright for the status of this software.
8 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 *
Owen Taylor3473f882001-02-23 17:55:21 +000011 */
12
Daniel Veillard34ce8be2002-03-18 19:37:11 +000013#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000014#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000015
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <string.h> /* for memset() only ! */
17
18#ifdef HAVE_CTYPE_H
19#include <ctype.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24#ifdef HAVE_ZLIB_H
25#include <zlib.h>
26#endif
27
28#include <libxml/xmlmemory.h>
29#include <libxml/tree.h>
30#include <libxml/parser.h>
Daniel Veillardb8c9be92001-07-09 16:01:19 +000031#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000032#include <libxml/entities.h>
33#include <libxml/valid.h>
34#include <libxml/xmlerror.h>
Daniel Veillardbdb9ba72001-04-11 11:28:06 +000035#include <libxml/parserInternals.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000036#include <libxml/globals.h>
Daniel Veillardd5c2f922002-11-21 14:10:52 +000037#ifdef LIBXML_HTML_ENABLED
38#include <libxml/HTMLtree.h>
39#endif
Owen Taylor3473f882001-02-23 17:55:21 +000040
Daniel Veillarda880b122003-04-21 21:36:41 +000041int __xmlRegisterCallbacks = 0;
42
Daniel Veillard56a4cb82001-03-24 17:00:36 +000043xmlNsPtr xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns);
44
45/************************************************************************
46 * *
Daniel Veillard18ec16e2003-10-07 23:16:40 +000047 * Tree memory error handler *
48 * *
49 ************************************************************************/
50/**
51 * xmlTreeErrMemory:
52 * @extra: extra informations
53 *
54 * Handle an out of memory condition
55 */
56static void
57xmlTreeErrMemory(const char *extra)
58{
59 __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
60}
61
62/**
63 * xmlTreeErr:
64 * @code: the error number
65 * @extra: extra informations
66 *
67 * Handle an out of memory condition
68 */
69static void
70xmlTreeErr(int code, xmlNodePtr node, const char *extra)
71{
72 const char *msg = NULL;
73
74 switch(code) {
75 case XML_TREE_INVALID_HEX:
76 msg = "invalid hexadecimal character value";
77 break;
78 case XML_TREE_INVALID_DEC:
79 msg = "invalid decimal character value";
80 break;
81 case XML_TREE_UNTERMINATED_ENTITY:
82 msg = "unterminated entity reference %15s";
83 break;
84 default:
85 msg = "unexpected error number";
86 }
87 __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
88}
89
90/************************************************************************
91 * *
Daniel Veillard56a4cb82001-03-24 17:00:36 +000092 * A few static variables and macros *
93 * *
94 ************************************************************************/
Daniel Veillardd0463562001-10-13 09:15:48 +000095/* #undef xmlStringText */
Daniel Veillard22090732001-07-16 00:06:07 +000096const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +000097/* #undef xmlStringTextNoenc */
Daniel Veillard22090732001-07-16 00:06:07 +000098const xmlChar xmlStringTextNoenc[] =
Owen Taylor3473f882001-02-23 17:55:21 +000099 { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 };
Daniel Veillardd0463562001-10-13 09:15:48 +0000100/* #undef xmlStringComment */
Daniel Veillard22090732001-07-16 00:06:07 +0000101const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 };
102
Owen Taylor3473f882001-02-23 17:55:21 +0000103static int xmlCompressMode = 0;
104static int xmlCheckDTD = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000105
Owen Taylor3473f882001-02-23 17:55:21 +0000106#define UPDATE_LAST_CHILD_AND_PARENT(n) if ((n) != NULL) { \
107 xmlNodePtr ulccur = (n)->children; \
108 if (ulccur == NULL) { \
109 (n)->last = NULL; \
110 } else { \
111 while (ulccur->next != NULL) { \
112 ulccur->parent = (n); \
113 ulccur = ulccur->next; \
114 } \
115 ulccur->parent = (n); \
116 (n)->last = ulccur; \
117}}
118
119/* #define DEBUG_BUFFER */
120/* #define DEBUG_TREE */
121
122/************************************************************************
123 * *
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000124 * Functions to move to entities.c once the *
125 * API freeze is smoothen and they can be made public. *
126 * *
127 ************************************************************************/
128#include <libxml/hash.h>
129
Daniel Veillard652327a2003-09-29 18:02:38 +0000130#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000131/**
132 * xmlGetEntityFromDtd:
133 * @dtd: A pointer to the DTD to search
134 * @name: The entity name
135 *
136 * Do an entity lookup in the DTD entity hash table and
137 * return the corresponding entity, if found.
138 *
139 * Returns A pointer to the entity structure or NULL if not found.
140 */
141static xmlEntityPtr
142xmlGetEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
143 xmlEntitiesTablePtr table;
144
145 if((dtd != NULL) && (dtd->entities != NULL)) {
146 table = (xmlEntitiesTablePtr) dtd->entities;
147 return((xmlEntityPtr) xmlHashLookup(table, name));
148 /* return(xmlGetEntityFromTable(table, name)); */
149 }
150 return(NULL);
151}
152/**
153 * xmlGetParameterEntityFromDtd:
154 * @dtd: A pointer to the DTD to search
155 * @name: The entity name
156 *
157 * Do an entity lookup in the DTD pararmeter entity hash table and
158 * return the corresponding entity, if found.
159 *
160 * Returns A pointer to the entity structure or NULL if not found.
161 */
162static xmlEntityPtr
163xmlGetParameterEntityFromDtd(xmlDtdPtr dtd, const xmlChar *name) {
164 xmlEntitiesTablePtr table;
165
166 if ((dtd != NULL) && (dtd->pentities != NULL)) {
167 table = (xmlEntitiesTablePtr) dtd->pentities;
168 return((xmlEntityPtr) xmlHashLookup(table, name));
169 /* return(xmlGetEntityFromTable(table, name)); */
170 }
171 return(NULL);
172}
Daniel Veillard652327a2003-09-29 18:02:38 +0000173#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +0000174
175/************************************************************************
176 * *
Daniel Veillardc00cda82003-04-07 10:22:39 +0000177 * QName handling helper *
178 * *
179 ************************************************************************/
180
181/**
182 * xmlBuildQName:
183 * @ncname: the Name
184 * @prefix: the prefix
185 * @memory: preallocated memory
186 * @len: preallocated memory length
187 *
188 * Builds the QName @prefix:@ncname in @memory if there is enough space
189 * and prefix is not NULL nor empty, otherwise allocate a new string.
190 * If prefix is NULL or empty it returns ncname.
191 *
192 * Returns the new string which must be freed by the caller if different from
193 * @memory and @ncname or NULL in case of error
194 */
195xmlChar *
196xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix,
197 xmlChar *memory, int len) {
198 int lenn, lenp;
199 xmlChar *ret;
200
Daniel Veillard3b7840c2003-09-11 23:42:01 +0000201 if (ncname == NULL) return(NULL);
202 if (prefix == NULL) return((xmlChar *) ncname);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000203
204 lenn = strlen((char *) ncname);
205 lenp = strlen((char *) prefix);
206
207 if ((memory == NULL) || (len < lenn + lenp + 2)) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +0000208 ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000209 if (ret == NULL) {
210 xmlTreeErrMemory("building QName");
211 return(NULL);
212 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000213 } else {
214 ret = memory;
215 }
216 memcpy(&ret[0], prefix, lenp);
217 ret[lenp] = ':';
218 memcpy(&ret[lenp + 1], ncname, lenn);
219 ret[lenn + lenp + 1] = 0;
220 return(ret);
221}
222
223/**
224 * xmlSplitQName2:
225 * @name: the full QName
226 * @prefix: a xmlChar **
227 *
228 * parse an XML qualified name string
229 *
230 * [NS 5] QName ::= (Prefix ':')? LocalPart
231 *
232 * [NS 6] Prefix ::= NCName
233 *
234 * [NS 7] LocalPart ::= NCName
235 *
236 * Returns NULL if not a QName, otherwise the local part, and prefix
237 * is updated to get the Prefix if any.
238 */
239
240xmlChar *
241xmlSplitQName2(const xmlChar *name, xmlChar **prefix) {
242 int len = 0;
243 xmlChar *ret = NULL;
244
245 *prefix = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000246 if (name == NULL) return(NULL);
Daniel Veillardc00cda82003-04-07 10:22:39 +0000247
248#ifndef XML_XML_NAMESPACE
249 /* xml: prefix is not really a namespace */
250 if ((name[0] == 'x') && (name[1] == 'm') &&
251 (name[2] == 'l') && (name[3] == ':'))
252 return(NULL);
253#endif
254
255 /* nasty but valid */
256 if (name[0] == ':')
257 return(NULL);
258
259 /*
260 * we are not trying to validate but just to cut, and yes it will
261 * work even if this is as set of UTF-8 encoded chars
262 */
263 while ((name[len] != 0) && (name[len] != ':'))
264 len++;
265
266 if (name[len] == 0)
267 return(NULL);
268
269 *prefix = xmlStrndup(name, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000270 if (*prefix == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000271 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000272 return(NULL);
273 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000274 ret = xmlStrdup(&name[len + 1]);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000275 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000276 xmlTreeErrMemory("QName split");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000277 if (*prefix != NULL) {
278 xmlFree(*prefix);
279 *prefix = NULL;
280 }
281 return(NULL);
282 }
Daniel Veillardc00cda82003-04-07 10:22:39 +0000283
284 return(ret);
285}
286
Daniel Veillard8d73bcb2003-08-04 01:06:15 +0000287/**
288 * xmlSplitQName3:
289 * @name: the full QName
290 * @len: an int *
291 *
292 * parse an XML qualified name string,i
293 *
294 * returns NULL if it is not a Qualified Name, otherwise, update len
295 * with the lenght in byte of the prefix and return a pointer
296 */
297
298const xmlChar *
299xmlSplitQName3(const xmlChar *name, int *len) {
300 int l = 0;
301
302 if (name == NULL) return(NULL);
303 if (len == NULL) return(NULL);
304
305 /* nasty but valid */
306 if (name[0] == ':')
307 return(NULL);
308
309 /*
310 * we are not trying to validate but just to cut, and yes it will
311 * work even if this is as set of UTF-8 encoded chars
312 */
313 while ((name[l] != 0) && (name[l] != ':'))
314 l++;
315
316 if (name[l] == 0)
317 return(NULL);
318
319 *len = l;
320
321 return(&name[l+1]);
322}
323
Daniel Veillard652327a2003-09-29 18:02:38 +0000324#ifdef LIBXML_TREE_ENABLED
Daniel Veillardc00cda82003-04-07 10:22:39 +0000325/************************************************************************
326 * *
Daniel Veillardd2298792003-02-14 16:54:11 +0000327 * Check Name, NCName and QName strings *
328 * *
329 ************************************************************************/
330
331#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
332
333/**
334 * xmlValidateNCName:
335 * @value: the value to check
336 * @space: allow spaces in front and end of the string
337 *
338 * Check that a value conforms to the lexical space of NCName
339 *
340 * Returns 0 if this validates, a positive error code number otherwise
341 * and -1 in case of internal or API error.
342 */
343int
344xmlValidateNCName(const xmlChar *value, int space) {
345 const xmlChar *cur = value;
346 int c,l;
347
348 /*
349 * First quick algorithm for ASCII range
350 */
351 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000352 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000353 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
354 (*cur == '_'))
355 cur++;
356 else
357 goto try_complex;
358 while (((*cur >= 'a') && (*cur <= 'z')) ||
359 ((*cur >= 'A') && (*cur <= 'Z')) ||
360 ((*cur >= '0') && (*cur <= '9')) ||
361 (*cur == '_') || (*cur == '-') || (*cur == '.'))
362 cur++;
363 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000364 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000365 if (*cur == 0)
366 return(0);
367
368try_complex:
369 /*
370 * Second check for chars outside the ASCII range
371 */
372 cur = value;
373 c = CUR_SCHAR(cur, l);
374 if (space) {
375 while (IS_BLANK(c)) {
376 cur += l;
377 c = CUR_SCHAR(cur, l);
378 }
379 }
William M. Brack871611b2003-10-18 04:53:14 +0000380 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000381 return(1);
382 cur += l;
383 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000384 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
385 (c == '-') || (c == '_') || IS_COMBINING(c) ||
386 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000387 cur += l;
388 c = CUR_SCHAR(cur, l);
389 }
390 if (space) {
391 while (IS_BLANK(c)) {
392 cur += l;
393 c = CUR_SCHAR(cur, l);
394 }
395 }
396 if (c != 0)
397 return(1);
398
399 return(0);
400}
401
402/**
403 * xmlValidateQName:
404 * @value: the value to check
405 * @space: allow spaces in front and end of the string
406 *
407 * Check that a value conforms to the lexical space of QName
408 *
409 * Returns 0 if this validates, a positive error code number otherwise
410 * and -1 in case of internal or API error.
411 */
412int
413xmlValidateQName(const xmlChar *value, int space) {
414 const xmlChar *cur = value;
415 int c,l;
416
417 /*
418 * First quick algorithm for ASCII range
419 */
420 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000421 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000422 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
423 (*cur == '_'))
424 cur++;
425 else
426 goto try_complex;
427 while (((*cur >= 'a') && (*cur <= 'z')) ||
428 ((*cur >= 'A') && (*cur <= 'Z')) ||
429 ((*cur >= '0') && (*cur <= '9')) ||
430 (*cur == '_') || (*cur == '-') || (*cur == '.'))
431 cur++;
432 if (*cur == ':') {
433 cur++;
434 if (((*cur >= 'a') && (*cur <= 'z')) ||
435 ((*cur >= 'A') && (*cur <= 'Z')) ||
436 (*cur == '_'))
437 cur++;
438 else
439 goto try_complex;
440 while (((*cur >= 'a') && (*cur <= 'z')) ||
441 ((*cur >= 'A') && (*cur <= 'Z')) ||
442 ((*cur >= '0') && (*cur <= '9')) ||
443 (*cur == '_') || (*cur == '-') || (*cur == '.'))
444 cur++;
445 }
446 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000447 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000448 if (*cur == 0)
449 return(0);
450
451try_complex:
452 /*
453 * Second check for chars outside the ASCII range
454 */
455 cur = value;
456 c = CUR_SCHAR(cur, l);
457 if (space) {
458 while (IS_BLANK(c)) {
459 cur += l;
460 c = CUR_SCHAR(cur, l);
461 }
462 }
William M. Brack871611b2003-10-18 04:53:14 +0000463 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000464 return(1);
465 cur += l;
466 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000467 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
468 (c == '-') || (c == '_') || IS_COMBINING(c) ||
469 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000470 cur += l;
471 c = CUR_SCHAR(cur, l);
472 }
473 if (c == ':') {
474 cur += l;
475 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000476 if ((!IS_LETTER(c)) && (c != '_'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000477 return(1);
478 cur += l;
479 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000480 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') ||
481 (c == '-') || (c == '_') || IS_COMBINING(c) ||
482 IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000483 cur += l;
484 c = CUR_SCHAR(cur, l);
485 }
486 }
487 if (space) {
488 while (IS_BLANK(c)) {
489 cur += l;
490 c = CUR_SCHAR(cur, l);
491 }
492 }
493 if (c != 0)
494 return(1);
495 return(0);
496}
497
498/**
499 * xmlValidateName:
500 * @value: the value to check
501 * @space: allow spaces in front and end of the string
502 *
503 * Check that a value conforms to the lexical space of Name
504 *
505 * Returns 0 if this validates, a positive error code number otherwise
506 * and -1 in case of internal or API error.
507 */
508int
509xmlValidateName(const xmlChar *value, int space) {
510 const xmlChar *cur = value;
511 int c,l;
512
513 /*
514 * First quick algorithm for ASCII range
515 */
516 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000517 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000518 if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) ||
519 (*cur == '_') || (*cur == ':'))
520 cur++;
521 else
522 goto try_complex;
523 while (((*cur >= 'a') && (*cur <= 'z')) ||
524 ((*cur >= 'A') && (*cur <= 'Z')) ||
525 ((*cur >= '0') && (*cur <= '9')) ||
526 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
527 cur++;
528 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000529 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +0000530 if (*cur == 0)
531 return(0);
532
533try_complex:
534 /*
535 * Second check for chars outside the ASCII range
536 */
537 cur = value;
538 c = CUR_SCHAR(cur, l);
539 if (space) {
540 while (IS_BLANK(c)) {
541 cur += l;
542 c = CUR_SCHAR(cur, l);
543 }
544 }
William M. Brack871611b2003-10-18 04:53:14 +0000545 if ((!IS_LETTER(c)) && (c != '_') && (c != ':'))
Daniel Veillardd2298792003-02-14 16:54:11 +0000546 return(1);
547 cur += l;
548 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000549 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
550 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd2298792003-02-14 16:54:11 +0000551 cur += l;
552 c = CUR_SCHAR(cur, l);
553 }
554 if (space) {
555 while (IS_BLANK(c)) {
556 cur += l;
557 c = CUR_SCHAR(cur, l);
558 }
559 }
560 if (c != 0)
561 return(1);
562 return(0);
563}
564
Daniel Veillardd4310742003-02-18 21:12:46 +0000565/**
566 * xmlValidateNMToken:
567 * @value: the value to check
568 * @space: allow spaces in front and end of the string
569 *
570 * Check that a value conforms to the lexical space of NMToken
571 *
572 * Returns 0 if this validates, a positive error code number otherwise
573 * and -1 in case of internal or API error.
574 */
575int
576xmlValidateNMToken(const xmlChar *value, int space) {
577 const xmlChar *cur = value;
578 int c,l;
579
580 /*
581 * First quick algorithm for ASCII range
582 */
583 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000584 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000585 if (((*cur >= 'a') && (*cur <= 'z')) ||
586 ((*cur >= 'A') && (*cur <= 'Z')) ||
587 ((*cur >= '0') && (*cur <= '9')) ||
588 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
589 cur++;
590 else
591 goto try_complex;
592 while (((*cur >= 'a') && (*cur <= 'z')) ||
593 ((*cur >= 'A') && (*cur <= 'Z')) ||
594 ((*cur >= '0') && (*cur <= '9')) ||
595 (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':'))
596 cur++;
597 if (space)
William M. Brack76e95df2003-10-18 16:20:14 +0000598 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardd4310742003-02-18 21:12:46 +0000599 if (*cur == 0)
600 return(0);
601
602try_complex:
603 /*
604 * Second check for chars outside the ASCII range
605 */
606 cur = value;
607 c = CUR_SCHAR(cur, l);
608 if (space) {
609 while (IS_BLANK(c)) {
610 cur += l;
611 c = CUR_SCHAR(cur, l);
612 }
613 }
William M. Brack871611b2003-10-18 04:53:14 +0000614 if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
615 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)))
Daniel Veillardd4310742003-02-18 21:12:46 +0000616 return(1);
617 cur += l;
618 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +0000619 while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') ||
620 (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) {
Daniel Veillardd4310742003-02-18 21:12:46 +0000621 cur += l;
622 c = CUR_SCHAR(cur, l);
623 }
624 if (space) {
625 while (IS_BLANK(c)) {
626 cur += l;
627 c = CUR_SCHAR(cur, l);
628 }
629 }
630 if (c != 0)
631 return(1);
632 return(0);
633}
Daniel Veillard652327a2003-09-29 18:02:38 +0000634#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardd4310742003-02-18 21:12:46 +0000635
Daniel Veillardd2298792003-02-14 16:54:11 +0000636/************************************************************************
637 * *
Owen Taylor3473f882001-02-23 17:55:21 +0000638 * Allocation and deallocation of basic structures *
639 * *
640 ************************************************************************/
641
642/**
643 * xmlSetBufferAllocationScheme:
644 * @scheme: allocation method to use
645 *
646 * Set the buffer allocation method. Types are
647 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
648 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
649 * improves performance
650 */
651void
652xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) {
653 xmlBufferAllocScheme = scheme;
654}
655
656/**
657 * xmlGetBufferAllocationScheme:
658 *
659 * Types are
660 * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down
661 * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed,
662 * improves performance
663 *
664 * Returns the current allocation scheme
665 */
666xmlBufferAllocationScheme
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000667xmlGetBufferAllocationScheme(void) {
Daniel Veillarde043ee12001-04-16 14:08:07 +0000668 return(xmlBufferAllocScheme);
Owen Taylor3473f882001-02-23 17:55:21 +0000669}
670
671/**
672 * xmlNewNs:
673 * @node: the element carrying the namespace
674 * @href: the URI associated
675 * @prefix: the prefix for the namespace
676 *
677 * Creation of a new Namespace. This function will refuse to create
678 * a namespace with a similar prefix than an existing one present on this
679 * node.
680 * We use href==NULL in the case of an element creation where the namespace
681 * was not defined.
Daniel Veillardd1640922001-12-17 15:30:10 +0000682 * Returns a new namespace pointer or NULL
Owen Taylor3473f882001-02-23 17:55:21 +0000683 */
684xmlNsPtr
685xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) {
686 xmlNsPtr cur;
687
688 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
689 return(NULL);
690
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000691 if ((prefix != NULL) && (xmlStrEqual(prefix, BAD_CAST "xml")))
692 return(NULL);
693
Owen Taylor3473f882001-02-23 17:55:21 +0000694 /*
695 * Allocate a new Namespace and fill the fields.
696 */
697 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
698 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000699 xmlTreeErrMemory("building namespace");
Owen Taylor3473f882001-02-23 17:55:21 +0000700 return(NULL);
701 }
702 memset(cur, 0, sizeof(xmlNs));
703 cur->type = XML_LOCAL_NAMESPACE;
704
705 if (href != NULL)
706 cur->href = xmlStrdup(href);
707 if (prefix != NULL)
708 cur->prefix = xmlStrdup(prefix);
709
710 /*
711 * Add it at the end to preserve parsing order ...
712 * and checks for existing use of the prefix
713 */
714 if (node != NULL) {
715 if (node->nsDef == NULL) {
716 node->nsDef = cur;
717 } else {
718 xmlNsPtr prev = node->nsDef;
719
720 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
721 (xmlStrEqual(prev->prefix, cur->prefix))) {
722 xmlFreeNs(cur);
723 return(NULL);
724 }
725 while (prev->next != NULL) {
726 prev = prev->next;
727 if (((prev->prefix == NULL) && (cur->prefix == NULL)) ||
728 (xmlStrEqual(prev->prefix, cur->prefix))) {
729 xmlFreeNs(cur);
730 return(NULL);
731 }
732 }
733 prev->next = cur;
734 }
735 }
736 return(cur);
737}
738
739/**
740 * xmlSetNs:
741 * @node: a node in the document
742 * @ns: a namespace pointer
743 *
744 * Associate a namespace to a node, a posteriori.
745 */
746void
747xmlSetNs(xmlNodePtr node, xmlNsPtr ns) {
748 if (node == NULL) {
749#ifdef DEBUG_TREE
750 xmlGenericError(xmlGenericErrorContext,
751 "xmlSetNs: node == NULL\n");
752#endif
753 return;
754 }
755 node->ns = ns;
756}
757
758/**
759 * xmlFreeNs:
760 * @cur: the namespace pointer
761 *
762 * Free up the structures associated to a namespace
763 */
764void
765xmlFreeNs(xmlNsPtr cur) {
766 if (cur == NULL) {
767#ifdef DEBUG_TREE
768 xmlGenericError(xmlGenericErrorContext,
769 "xmlFreeNs : ns == NULL\n");
770#endif
771 return;
772 }
773 if (cur->href != NULL) xmlFree((char *) cur->href);
774 if (cur->prefix != NULL) xmlFree((char *) cur->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000775 xmlFree(cur);
776}
777
778/**
779 * xmlFreeNsList:
780 * @cur: the first namespace pointer
781 *
782 * Free up all the structures associated to the chained namespaces.
783 */
784void
785xmlFreeNsList(xmlNsPtr cur) {
786 xmlNsPtr next;
787 if (cur == NULL) {
788#ifdef DEBUG_TREE
789 xmlGenericError(xmlGenericErrorContext,
790 "xmlFreeNsList : ns == NULL\n");
791#endif
792 return;
793 }
794 while (cur != NULL) {
795 next = cur->next;
796 xmlFreeNs(cur);
797 cur = next;
798 }
799}
800
801/**
802 * xmlNewDtd:
803 * @doc: the document pointer
804 * @name: the DTD name
805 * @ExternalID: the external ID
806 * @SystemID: the system ID
807 *
808 * Creation of a new DTD for the external subset. To create an
809 * internal subset, use xmlCreateIntSubset().
810 *
811 * Returns a pointer to the new DTD structure
812 */
813xmlDtdPtr
814xmlNewDtd(xmlDocPtr doc, const xmlChar *name,
815 const xmlChar *ExternalID, const xmlChar *SystemID) {
816 xmlDtdPtr cur;
817
818 if ((doc != NULL) && (doc->extSubset != NULL)) {
819#ifdef DEBUG_TREE
820 xmlGenericError(xmlGenericErrorContext,
821 "xmlNewDtd(%s): document %s already have a DTD %s\n",
822 /* !!! */ (char *) name, doc->name,
823 /* !!! */ (char *)doc->extSubset->name);
824#endif
825 return(NULL);
826 }
827
828 /*
829 * Allocate a new DTD and fill the fields.
830 */
831 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
832 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000833 xmlTreeErrMemory("building DTD");
Owen Taylor3473f882001-02-23 17:55:21 +0000834 return(NULL);
835 }
836 memset(cur, 0 , sizeof(xmlDtd));
837 cur->type = XML_DTD_NODE;
838
839 if (name != NULL)
840 cur->name = xmlStrdup(name);
841 if (ExternalID != NULL)
842 cur->ExternalID = xmlStrdup(ExternalID);
843 if (SystemID != NULL)
844 cur->SystemID = xmlStrdup(SystemID);
845 if (doc != NULL)
846 doc->extSubset = cur;
847 cur->doc = doc;
848
Daniel Veillarda880b122003-04-21 21:36:41 +0000849 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000850 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000851 return(cur);
852}
853
854/**
855 * xmlGetIntSubset:
856 * @doc: the document pointer
857 *
858 * Get the internal subset of a document
859 * Returns a pointer to the DTD structure or NULL if not found
860 */
861
862xmlDtdPtr
863xmlGetIntSubset(xmlDocPtr doc) {
864 xmlNodePtr cur;
865
866 if (doc == NULL)
867 return(NULL);
868 cur = doc->children;
869 while (cur != NULL) {
870 if (cur->type == XML_DTD_NODE)
871 return((xmlDtdPtr) cur);
872 cur = cur->next;
873 }
874 return((xmlDtdPtr) doc->intSubset);
875}
876
877/**
878 * xmlCreateIntSubset:
879 * @doc: the document pointer
880 * @name: the DTD name
Daniel Veillarde356c282001-03-10 12:32:04 +0000881 * @ExternalID: the external (PUBLIC) ID
Owen Taylor3473f882001-02-23 17:55:21 +0000882 * @SystemID: the system ID
883 *
884 * Create the internal subset of a document
885 * Returns a pointer to the new DTD structure
886 */
887xmlDtdPtr
888xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name,
889 const xmlChar *ExternalID, const xmlChar *SystemID) {
890 xmlDtdPtr cur;
891
892 if ((doc != NULL) && (xmlGetIntSubset(doc) != NULL)) {
893#ifdef DEBUG_TREE
894 xmlGenericError(xmlGenericErrorContext,
895
896 "xmlCreateIntSubset(): document %s already have an internal subset\n",
897 doc->name);
898#endif
899 return(NULL);
900 }
901
902 /*
903 * Allocate a new DTD and fill the fields.
904 */
905 cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
906 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +0000907 xmlTreeErrMemory("building internal subset");
Owen Taylor3473f882001-02-23 17:55:21 +0000908 return(NULL);
909 }
910 memset(cur, 0, sizeof(xmlDtd));
911 cur->type = XML_DTD_NODE;
912
913 if (name != NULL)
914 cur->name = xmlStrdup(name);
915 if (ExternalID != NULL)
916 cur->ExternalID = xmlStrdup(ExternalID);
917 if (SystemID != NULL)
918 cur->SystemID = xmlStrdup(SystemID);
919 if (doc != NULL) {
920 doc->intSubset = cur;
921 cur->parent = doc;
922 cur->doc = doc;
923 if (doc->children == NULL) {
924 doc->children = (xmlNodePtr) cur;
925 doc->last = (xmlNodePtr) cur;
926 } else {
Owen Taylor3473f882001-02-23 17:55:21 +0000927 if (doc->type == XML_HTML_DOCUMENT_NODE) {
Daniel Veillarde356c282001-03-10 12:32:04 +0000928 xmlNodePtr prev;
929
Owen Taylor3473f882001-02-23 17:55:21 +0000930 prev = doc->children;
931 prev->prev = (xmlNodePtr) cur;
932 cur->next = prev;
933 doc->children = (xmlNodePtr) cur;
934 } else {
Daniel Veillarde356c282001-03-10 12:32:04 +0000935 xmlNodePtr next;
936
937 next = doc->children;
938 while ((next != NULL) && (next->type != XML_ELEMENT_NODE))
939 next = next->next;
940 if (next == NULL) {
941 cur->prev = doc->last;
942 cur->prev->next = (xmlNodePtr) cur;
943 cur->next = NULL;
944 doc->last = (xmlNodePtr) cur;
945 } else {
946 cur->next = next;
947 cur->prev = next->prev;
948 if (cur->prev == NULL)
949 doc->children = (xmlNodePtr) cur;
950 else
951 cur->prev->next = (xmlNodePtr) cur;
952 next->prev = (xmlNodePtr) cur;
953 }
Owen Taylor3473f882001-02-23 17:55:21 +0000954 }
955 }
956 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000957
Daniel Veillarda880b122003-04-21 21:36:41 +0000958 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +0000959 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000960 return(cur);
961}
962
963/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000964 * DICT_FREE:
965 * @str: a string
966 *
967 * Free a string if it is not owned by the "dict" dictionnary in the
968 * current scope
969 */
970#define DICT_FREE(str) \
971 if ((str) && ((!dict) || \
972 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
973 xmlFree((char *)(str));
974
975/**
Owen Taylor3473f882001-02-23 17:55:21 +0000976 * xmlFreeDtd:
977 * @cur: the DTD structure to free up
978 *
979 * Free a DTD structure.
980 */
981void
982xmlFreeDtd(xmlDtdPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000983 xmlDictPtr dict = NULL;
984
Owen Taylor3473f882001-02-23 17:55:21 +0000985 if (cur == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000986 return;
987 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +0000988 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +0000989
Daniel Veillarda880b122003-04-21 21:36:41 +0000990 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +0000991 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
992
Owen Taylor3473f882001-02-23 17:55:21 +0000993 if (cur->children != NULL) {
994 xmlNodePtr next, c = cur->children;
995
996 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000997 * Cleanup all the DTD comments they are not in the DTD
Owen Taylor3473f882001-02-23 17:55:21 +0000998 * indexes.
999 */
1000 while (c != NULL) {
1001 next = c->next;
Daniel Veillardd72c7e32003-05-12 21:55:03 +00001002 if ((c->type == XML_COMMENT_NODE) || (c->type == XML_PI_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001003 xmlUnlinkNode(c);
1004 xmlFreeNode(c);
1005 }
1006 c = next;
1007 }
1008 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001009 DICT_FREE(cur->name)
1010 DICT_FREE(cur->SystemID)
1011 DICT_FREE(cur->ExternalID)
Owen Taylor3473f882001-02-23 17:55:21 +00001012 /* TODO !!! */
1013 if (cur->notations != NULL)
1014 xmlFreeNotationTable((xmlNotationTablePtr) cur->notations);
1015
1016 if (cur->elements != NULL)
1017 xmlFreeElementTable((xmlElementTablePtr) cur->elements);
1018 if (cur->attributes != NULL)
1019 xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes);
1020 if (cur->entities != NULL)
1021 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities);
1022 if (cur->pentities != NULL)
1023 xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities);
1024
Owen Taylor3473f882001-02-23 17:55:21 +00001025 xmlFree(cur);
1026}
1027
1028/**
1029 * xmlNewDoc:
1030 * @version: xmlChar string giving the version of XML "1.0"
1031 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001032 * Creates a new XML document
1033 *
Owen Taylor3473f882001-02-23 17:55:21 +00001034 * Returns a new document
1035 */
1036xmlDocPtr
1037xmlNewDoc(const xmlChar *version) {
1038 xmlDocPtr cur;
1039
1040 if (version == NULL)
1041 version = (const xmlChar *) "1.0";
1042
1043 /*
1044 * Allocate a new document and fill the fields.
1045 */
1046 cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1047 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001048 xmlTreeErrMemory("building doc");
Owen Taylor3473f882001-02-23 17:55:21 +00001049 return(NULL);
1050 }
1051 memset(cur, 0, sizeof(xmlDoc));
1052 cur->type = XML_DOCUMENT_NODE;
1053
1054 cur->version = xmlStrdup(version);
1055 cur->standalone = -1;
1056 cur->compression = -1; /* not initialized */
1057 cur->doc = cur;
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001058 /*
1059 * The in memory encoding is always UTF8
1060 * This field will never change and would
1061 * be obsolete if not for binary compatibility.
1062 */
Daniel Veillardd2f3ec72001-04-11 07:50:02 +00001063 cur->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001064
Daniel Veillarda880b122003-04-21 21:36:41 +00001065 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001066 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001067 return(cur);
1068}
1069
1070/**
1071 * xmlFreeDoc:
1072 * @cur: pointer to the document
Owen Taylor3473f882001-02-23 17:55:21 +00001073 *
1074 * Free up all the structures used by a document, tree included.
1075 */
1076void
1077xmlFreeDoc(xmlDocPtr cur) {
Daniel Veillarda9142e72001-06-19 11:07:54 +00001078 xmlDtdPtr extSubset, intSubset;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001079 xmlDictPtr dict = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001080
Owen Taylor3473f882001-02-23 17:55:21 +00001081 if (cur == NULL) {
1082#ifdef DEBUG_TREE
1083 xmlGenericError(xmlGenericErrorContext,
1084 "xmlFreeDoc : document == NULL\n");
1085#endif
1086 return;
1087 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001088 if (cur != NULL) dict = cur->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001089
Daniel Veillarda880b122003-04-21 21:36:41 +00001090 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001091 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1092
Daniel Veillard76d66f42001-05-16 21:05:17 +00001093 /*
1094 * Do this before freeing the children list to avoid ID lookups
1095 */
1096 if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids);
1097 cur->ids = NULL;
1098 if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs);
1099 cur->refs = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001100 extSubset = cur->extSubset;
1101 intSubset = cur->intSubset;
Daniel Veillard5997aca2002-03-18 18:36:20 +00001102 if (intSubset == extSubset)
1103 extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001104 if (extSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001105 xmlUnlinkNode((xmlNodePtr) cur->extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001106 cur->extSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001107 xmlFreeDtd(extSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001108 }
Daniel Veillarda9142e72001-06-19 11:07:54 +00001109 if (intSubset != NULL) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00001110 xmlUnlinkNode((xmlNodePtr) cur->intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001111 cur->intSubset = NULL;
Daniel Veillarda9142e72001-06-19 11:07:54 +00001112 xmlFreeDtd(intSubset);
Daniel Veillard76d66f42001-05-16 21:05:17 +00001113 }
1114
1115 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Owen Taylor3473f882001-02-23 17:55:21 +00001116 if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001117
1118 DICT_FREE(cur->version)
1119 DICT_FREE(cur->name)
1120 DICT_FREE(cur->encoding)
1121 DICT_FREE(cur->URL)
Owen Taylor3473f882001-02-23 17:55:21 +00001122 xmlFree(cur);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001123 if (dict) xmlDictFree(dict);
Owen Taylor3473f882001-02-23 17:55:21 +00001124}
1125
1126/**
1127 * xmlStringLenGetNodeList:
1128 * @doc: the document
1129 * @value: the value of the text
1130 * @len: the length of the string value
1131 *
1132 * Parse the value string and build the node list associated. Should
1133 * produce a flat tree with only TEXTs and ENTITY_REFs.
1134 * Returns a pointer to the first child
1135 */
1136xmlNodePtr
1137xmlStringLenGetNodeList(xmlDocPtr doc, const xmlChar *value, int len) {
1138 xmlNodePtr ret = NULL, last = NULL;
1139 xmlNodePtr node;
1140 xmlChar *val;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001141 const xmlChar *cur = value, *end = cur + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001142 const xmlChar *q;
1143 xmlEntityPtr ent;
1144
1145 if (value == NULL) return(NULL);
1146
1147 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001148 while ((cur < end) && (*cur != 0)) {
1149 if (cur[0] == '&') {
1150 int charval = 0;
1151 xmlChar tmp;
1152
Owen Taylor3473f882001-02-23 17:55:21 +00001153 /*
1154 * Save the current text.
1155 */
1156 if (cur != q) {
1157 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1158 xmlNodeAddContentLen(last, q, cur - q);
1159 } else {
1160 node = xmlNewDocTextLen(doc, q, cur - q);
1161 if (node == NULL) return(ret);
1162 if (last == NULL)
1163 last = ret = node;
1164 else {
1165 last->next = node;
1166 node->prev = last;
1167 last = node;
1168 }
1169 }
1170 }
Owen Taylor3473f882001-02-23 17:55:21 +00001171 q = cur;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001172 if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) {
1173 cur += 3;
1174 if (cur < end)
1175 tmp = *cur;
1176 else
1177 tmp = 0;
1178 while (tmp != ';') { /* Non input consuming loop */
1179 if ((tmp >= '0') && (tmp <= '9'))
1180 charval = charval * 16 + (tmp - '0');
1181 else if ((tmp >= 'a') && (tmp <= 'f'))
1182 charval = charval * 16 + (tmp - 'a') + 10;
1183 else if ((tmp >= 'A') && (tmp <= 'F'))
1184 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001185 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001186 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1187 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001188 charval = 0;
1189 break;
1190 }
1191 cur++;
1192 if (cur < end)
1193 tmp = *cur;
1194 else
1195 tmp = 0;
1196 }
1197 if (tmp == ';')
1198 cur++;
1199 q = cur;
1200 } else if ((cur + 1 < end) && (cur[1] == '#')) {
1201 cur += 2;
1202 if (cur < end)
1203 tmp = *cur;
1204 else
1205 tmp = 0;
1206 while (tmp != ';') { /* Non input consuming loops */
1207 if ((tmp >= '0') && (tmp <= '9'))
1208 charval = charval * 10 + (tmp - '0');
1209 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001210 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1211 NULL);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001212 charval = 0;
1213 break;
1214 }
1215 cur++;
1216 if (cur < end)
1217 tmp = *cur;
1218 else
1219 tmp = 0;
1220 }
1221 if (tmp == ';')
1222 cur++;
1223 q = cur;
1224 } else {
1225 /*
1226 * Read the entity string
1227 */
1228 cur++;
1229 q = cur;
1230 while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
1231 if ((cur >= end) || (*cur == 0)) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001232 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
1233 (const char *) q);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001234 return(ret);
1235 }
1236 if (cur != q) {
1237 /*
1238 * Predefined entities don't generate nodes
1239 */
1240 val = xmlStrndup(q, cur - q);
1241 ent = xmlGetDocEntity(doc, val);
1242 if ((ent != NULL) &&
1243 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1244 if (last == NULL) {
1245 node = xmlNewDocText(doc, ent->content);
1246 last = ret = node;
1247 } else if (last->type != XML_TEXT_NODE) {
1248 node = xmlNewDocText(doc, ent->content);
1249 last = xmlAddNextSibling(last, node);
1250 } else
1251 xmlNodeAddContent(last, ent->content);
1252
1253 } else {
1254 /*
1255 * Create a new REFERENCE_REF node
1256 */
1257 node = xmlNewReference(doc, val);
1258 if (node == NULL) {
1259 if (val != NULL) xmlFree(val);
1260 return(ret);
1261 }
1262 else if ((ent != NULL) && (ent->children == NULL)) {
1263 xmlNodePtr temp;
1264
1265 ent->children = xmlStringGetNodeList(doc,
1266 (const xmlChar*)node->content);
1267 ent->owner = 1;
1268 temp = ent->children;
1269 while (temp) {
1270 temp->parent = (xmlNodePtr)ent;
1271 temp = temp->next;
1272 }
1273 }
1274 if (last == NULL) {
1275 last = ret = node;
1276 } else {
1277 last = xmlAddNextSibling(last, node);
1278 }
1279 }
1280 xmlFree(val);
1281 }
1282 cur++;
1283 q = cur;
1284 }
1285 if (charval != 0) {
1286 xmlChar buf[10];
1287 int l;
1288
1289 l = xmlCopyCharMultiByte(buf, charval);
1290 buf[l] = 0;
1291 node = xmlNewDocText(doc, buf);
1292 if (node != NULL) {
1293 if (last == NULL) {
1294 last = ret = node;
1295 } else {
1296 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001297 }
1298 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001299 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001300 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001301 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001302 cur++;
1303 }
Daniel Veillard07cb8222003-09-10 10:51:05 +00001304 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001305 /*
1306 * Handle the last piece of text.
1307 */
1308 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1309 xmlNodeAddContentLen(last, q, cur - q);
1310 } else {
1311 node = xmlNewDocTextLen(doc, q, cur - q);
1312 if (node == NULL) return(ret);
Daniel Veillard07cb8222003-09-10 10:51:05 +00001313 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001314 last = ret = node;
Daniel Veillard07cb8222003-09-10 10:51:05 +00001315 } else {
1316 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001317 }
1318 }
1319 }
1320 return(ret);
1321}
1322
1323/**
1324 * xmlStringGetNodeList:
1325 * @doc: the document
1326 * @value: the value of the attribute
1327 *
1328 * Parse the value string and build the node list associated. Should
1329 * produce a flat tree with only TEXTs and ENTITY_REFs.
1330 * Returns a pointer to the first child
1331 */
1332xmlNodePtr
1333xmlStringGetNodeList(xmlDocPtr doc, const xmlChar *value) {
1334 xmlNodePtr ret = NULL, last = NULL;
1335 xmlNodePtr node;
1336 xmlChar *val;
1337 const xmlChar *cur = value;
1338 const xmlChar *q;
1339 xmlEntityPtr ent;
1340
1341 if (value == NULL) return(NULL);
1342
1343 q = cur;
1344 while (*cur != 0) {
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001345 if (cur[0] == '&') {
1346 int charval = 0;
1347 xmlChar tmp;
1348
Owen Taylor3473f882001-02-23 17:55:21 +00001349 /*
1350 * Save the current text.
1351 */
1352 if (cur != q) {
1353 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1354 xmlNodeAddContentLen(last, q, cur - q);
1355 } else {
1356 node = xmlNewDocTextLen(doc, q, cur - q);
1357 if (node == NULL) return(ret);
1358 if (last == NULL)
1359 last = ret = node;
1360 else {
1361 last->next = node;
1362 node->prev = last;
1363 last = node;
1364 }
1365 }
1366 }
Owen Taylor3473f882001-02-23 17:55:21 +00001367 q = cur;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001368 if ((cur[1] == '#') && (cur[2] == 'x')) {
1369 cur += 3;
1370 tmp = *cur;
1371 while (tmp != ';') { /* Non input consuming loop */
1372 if ((tmp >= '0') && (tmp <= '9'))
1373 charval = charval * 16 + (tmp - '0');
1374 else if ((tmp >= 'a') && (tmp <= 'f'))
1375 charval = charval * 16 + (tmp - 'a') + 10;
1376 else if ((tmp >= 'A') && (tmp <= 'F'))
1377 charval = charval * 16 + (tmp - 'A') + 10;
Owen Taylor3473f882001-02-23 17:55:21 +00001378 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001379 xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
1380 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001381 charval = 0;
1382 break;
1383 }
1384 cur++;
1385 tmp = *cur;
1386 }
1387 if (tmp == ';')
1388 cur++;
1389 q = cur;
1390 } else if (cur[1] == '#') {
1391 cur += 2;
1392 tmp = *cur;
1393 while (tmp != ';') { /* Non input consuming loops */
1394 if ((tmp >= '0') && (tmp <= '9'))
1395 charval = charval * 10 + (tmp - '0');
1396 else {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001397 xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
1398 NULL);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001399 charval = 0;
1400 break;
1401 }
1402 cur++;
1403 tmp = *cur;
1404 }
1405 if (tmp == ';')
1406 cur++;
1407 q = cur;
1408 } else {
1409 /*
1410 * Read the entity string
1411 */
1412 cur++;
1413 q = cur;
1414 while ((*cur != 0) && (*cur != ';')) cur++;
1415 if (*cur == 0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001416 xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
1417 (xmlNodePtr) doc, (const char *) q);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001418 return(ret);
1419 }
1420 if (cur != q) {
1421 /*
1422 * Predefined entities don't generate nodes
1423 */
1424 val = xmlStrndup(q, cur - q);
1425 ent = xmlGetDocEntity(doc, val);
1426 if ((ent != NULL) &&
1427 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1428 if (last == NULL) {
1429 node = xmlNewDocText(doc, ent->content);
1430 last = ret = node;
Daniel Veillard6f42c132002-01-06 23:05:13 +00001431 } else if (last->type != XML_TEXT_NODE) {
1432 node = xmlNewDocText(doc, ent->content);
1433 last = xmlAddNextSibling(last, node);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001434 } else
1435 xmlNodeAddContent(last, ent->content);
1436
1437 } else {
1438 /*
1439 * Create a new REFERENCE_REF node
1440 */
1441 node = xmlNewReference(doc, val);
1442 if (node == NULL) {
1443 if (val != NULL) xmlFree(val);
1444 return(ret);
1445 }
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001446 else if ((ent != NULL) && (ent->children == NULL)) {
1447 xmlNodePtr temp;
1448
1449 ent->children = xmlStringGetNodeList(doc,
1450 (const xmlChar*)node->content);
Daniel Veillard2d84a892002-12-30 00:01:08 +00001451 ent->owner = 1;
Daniel Veillardbf8dae82002-04-18 16:39:10 +00001452 temp = ent->children;
1453 while (temp) {
1454 temp->parent = (xmlNodePtr)ent;
1455 temp = temp->next;
1456 }
1457 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001458 if (last == NULL) {
1459 last = ret = node;
1460 } else {
1461 last = xmlAddNextSibling(last, node);
1462 }
1463 }
1464 xmlFree(val);
1465 }
1466 cur++;
1467 q = cur;
1468 }
1469 if (charval != 0) {
1470 xmlChar buf[10];
1471 int len;
1472
1473 len = xmlCopyCharMultiByte(buf, charval);
1474 buf[len] = 0;
1475 node = xmlNewDocText(doc, buf);
1476 if (node != NULL) {
1477 if (last == NULL) {
1478 last = ret = node;
1479 } else {
1480 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001481 }
1482 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001483
1484 charval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001485 }
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001486 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001487 cur++;
1488 }
Daniel Veillard75bea542001-05-11 17:41:21 +00001489 if ((cur != q) || (ret == NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001490 /*
1491 * Handle the last piece of text.
1492 */
1493 if ((last != NULL) && (last->type == XML_TEXT_NODE)) {
1494 xmlNodeAddContentLen(last, q, cur - q);
1495 } else {
1496 node = xmlNewDocTextLen(doc, q, cur - q);
1497 if (node == NULL) return(ret);
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001498 if (last == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001499 last = ret = node;
Daniel Veillardbdb9ba72001-04-11 11:28:06 +00001500 } else {
1501 last = xmlAddNextSibling(last, node);
Owen Taylor3473f882001-02-23 17:55:21 +00001502 }
1503 }
1504 }
1505 return(ret);
1506}
1507
1508/**
1509 * xmlNodeListGetString:
1510 * @doc: the document
1511 * @list: a Node list
1512 * @inLine: should we replace entity contents or show their external form
1513 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001514 * Build the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001515 * made of TEXTs and ENTITY_REFs
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001516 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001517 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001518 */
1519xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001520xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1521{
Owen Taylor3473f882001-02-23 17:55:21 +00001522 xmlNodePtr node = list;
1523 xmlChar *ret = NULL;
1524 xmlEntityPtr ent;
1525
Daniel Veillard7646b182002-04-20 06:41:40 +00001526 if (list == NULL)
1527 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001528
1529 while (node != NULL) {
1530 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001531 (node->type == XML_CDATA_SECTION_NODE)) {
1532 if (inLine) {
1533 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001534 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001535 xmlChar *buffer;
Owen Taylor3473f882001-02-23 17:55:21 +00001536
Daniel Veillard7646b182002-04-20 06:41:40 +00001537 buffer = xmlEncodeEntitiesReentrant(doc, node->content);
1538 if (buffer != NULL) {
1539 ret = xmlStrcat(ret, buffer);
1540 xmlFree(buffer);
1541 }
1542 }
1543 } else if (node->type == XML_ENTITY_REF_NODE) {
1544 if (inLine) {
1545 ent = xmlGetDocEntity(doc, node->name);
1546 if (ent != NULL) {
1547 xmlChar *buffer;
1548
1549 /* an entity content can be any "well balanced chunk",
1550 * i.e. the result of the content [43] production:
1551 * http://www.w3.org/TR/REC-xml#NT-content.
1552 * So it can contain text, CDATA section or nested
1553 * entity reference nodes (among others).
1554 * -> we recursive call xmlNodeListGetString()
1555 * which handles these types */
1556 buffer = xmlNodeListGetString(doc, ent->children, 1);
1557 if (buffer != NULL) {
1558 ret = xmlStrcat(ret, buffer);
1559 xmlFree(buffer);
1560 }
1561 } else {
1562 ret = xmlStrcat(ret, node->content);
1563 }
1564 } else {
1565 xmlChar buf[2];
1566
1567 buf[0] = '&';
1568 buf[1] = 0;
1569 ret = xmlStrncat(ret, buf, 1);
1570 ret = xmlStrcat(ret, node->name);
1571 buf[0] = ';';
1572 buf[1] = 0;
1573 ret = xmlStrncat(ret, buf, 1);
1574 }
1575 }
1576#if 0
1577 else {
1578 xmlGenericError(xmlGenericErrorContext,
1579 "xmlGetNodeListString : invalid node type %d\n",
1580 node->type);
1581 }
1582#endif
1583 node = node->next;
1584 }
1585 return (ret);
1586}
Daniel Veillard652327a2003-09-29 18:02:38 +00001587
1588#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001589/**
1590 * xmlNodeListGetRawString:
1591 * @doc: the document
1592 * @list: a Node list
1593 * @inLine: should we replace entity contents or show their external form
1594 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001595 * Builds the string equivalent to the text contained in the Node list
Owen Taylor3473f882001-02-23 17:55:21 +00001596 * made of TEXTs and ENTITY_REFs, contrary to xmlNodeListGetString()
1597 * this function doesn't do any character encoding handling.
1598 *
Daniel Veillardbd9afb52002-09-25 22:25:35 +00001599 * Returns a pointer to the string copy, the caller must free it with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00001600 */
1601xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00001602xmlNodeListGetRawString(xmlDocPtr doc, xmlNodePtr list, int inLine)
1603{
Owen Taylor3473f882001-02-23 17:55:21 +00001604 xmlNodePtr node = list;
1605 xmlChar *ret = NULL;
1606 xmlEntityPtr ent;
1607
Daniel Veillard7646b182002-04-20 06:41:40 +00001608 if (list == NULL)
1609 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001610
1611 while (node != NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00001612 if ((node->type == XML_TEXT_NODE) ||
Daniel Veillard7646b182002-04-20 06:41:40 +00001613 (node->type == XML_CDATA_SECTION_NODE)) {
1614 if (inLine) {
1615 ret = xmlStrcat(ret, node->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001616 } else {
Daniel Veillard7646b182002-04-20 06:41:40 +00001617 xmlChar *buffer;
1618
1619 buffer = xmlEncodeSpecialChars(doc, node->content);
1620 if (buffer != NULL) {
1621 ret = xmlStrcat(ret, buffer);
1622 xmlFree(buffer);
1623 }
1624 }
1625 } else if (node->type == XML_ENTITY_REF_NODE) {
1626 if (inLine) {
1627 ent = xmlGetDocEntity(doc, node->name);
1628 if (ent != NULL) {
1629 xmlChar *buffer;
1630
1631 /* an entity content can be any "well balanced chunk",
1632 * i.e. the result of the content [43] production:
1633 * http://www.w3.org/TR/REC-xml#NT-content.
1634 * So it can contain text, CDATA section or nested
1635 * entity reference nodes (among others).
1636 * -> we recursive call xmlNodeListGetRawString()
1637 * which handles these types */
1638 buffer =
1639 xmlNodeListGetRawString(doc, ent->children, 1);
1640 if (buffer != NULL) {
1641 ret = xmlStrcat(ret, buffer);
1642 xmlFree(buffer);
1643 }
1644 } else {
1645 ret = xmlStrcat(ret, node->content);
1646 }
1647 } else {
1648 xmlChar buf[2];
1649
1650 buf[0] = '&';
1651 buf[1] = 0;
1652 ret = xmlStrncat(ret, buf, 1);
1653 ret = xmlStrcat(ret, node->name);
1654 buf[0] = ';';
1655 buf[1] = 0;
1656 ret = xmlStrncat(ret, buf, 1);
1657 }
1658 }
Owen Taylor3473f882001-02-23 17:55:21 +00001659#if 0
Daniel Veillard7646b182002-04-20 06:41:40 +00001660 else {
1661 xmlGenericError(xmlGenericErrorContext,
1662 "xmlGetNodeListString : invalid node type %d\n",
1663 node->type);
1664 }
Owen Taylor3473f882001-02-23 17:55:21 +00001665#endif
Daniel Veillard7646b182002-04-20 06:41:40 +00001666 node = node->next;
Owen Taylor3473f882001-02-23 17:55:21 +00001667 }
Daniel Veillard7646b182002-04-20 06:41:40 +00001668 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001669}
Daniel Veillard652327a2003-09-29 18:02:38 +00001670#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001671
Daniel Veillard652327a2003-09-29 18:02:38 +00001672#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001673/**
1674 * xmlNewProp:
1675 * @node: the holding node
1676 * @name: the name of the attribute
1677 * @value: the value of the attribute
1678 *
1679 * Create a new property carried by a node.
1680 * Returns a pointer to the attribute
1681 */
1682xmlAttrPtr
1683xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
1684 xmlAttrPtr cur;
1685 xmlDocPtr doc = NULL;
1686
1687 if (name == NULL) {
1688#ifdef DEBUG_TREE
1689 xmlGenericError(xmlGenericErrorContext,
1690 "xmlNewProp : name == NULL\n");
1691#endif
1692 return(NULL);
1693 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00001694 if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
1695 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001696
1697 /*
1698 * Allocate a new property and fill the fields.
1699 */
1700 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1701 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001702 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001703 return(NULL);
1704 }
1705 memset(cur, 0, sizeof(xmlAttr));
1706 cur->type = XML_ATTRIBUTE_NODE;
1707
1708 cur->parent = node;
1709 if (node != NULL) {
1710 doc = node->doc;
1711 cur->doc = doc;
1712 }
1713 cur->name = xmlStrdup(name);
1714 if (value != NULL) {
1715 xmlChar *buffer;
1716 xmlNodePtr tmp;
1717
1718 buffer = xmlEncodeEntitiesReentrant(doc, value);
1719 cur->children = xmlStringGetNodeList(doc, buffer);
1720 cur->last = NULL;
1721 tmp = cur->children;
1722 while (tmp != NULL) {
1723 tmp->parent = (xmlNodePtr) cur;
1724 tmp->doc = doc;
1725 if (tmp->next == NULL)
1726 cur->last = tmp;
1727 tmp = tmp->next;
1728 }
1729 xmlFree(buffer);
1730 }
1731
1732 /*
1733 * Add it at the end to preserve parsing order ...
1734 */
1735 if (node != NULL) {
1736 if (node->properties == NULL) {
1737 node->properties = cur;
1738 } else {
1739 xmlAttrPtr prev = node->properties;
1740
1741 while (prev->next != NULL) prev = prev->next;
1742 prev->next = cur;
1743 cur->prev = prev;
1744 }
1745 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001746
Daniel Veillarda880b122003-04-21 21:36:41 +00001747 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001748 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001749 return(cur);
1750}
Daniel Veillard652327a2003-09-29 18:02:38 +00001751#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001752
1753/**
1754 * xmlNewNsProp:
1755 * @node: the holding node
1756 * @ns: the namespace
1757 * @name: the name of the attribute
1758 * @value: the value of the attribute
1759 *
1760 * Create a new property tagged with a namespace and carried by a node.
1761 * Returns a pointer to the attribute
1762 */
1763xmlAttrPtr
1764xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
1765 const xmlChar *value) {
1766 xmlAttrPtr cur;
Daniel Veillarda682b212001-06-07 19:59:42 +00001767 xmlDocPtr doc = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001768
1769 if (name == NULL) {
1770#ifdef DEBUG_TREE
1771 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001772 "xmlNewNsProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001773#endif
1774 return(NULL);
1775 }
1776
1777 /*
1778 * Allocate a new property and fill the fields.
1779 */
1780 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1781 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001782 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001783 return(NULL);
1784 }
1785 memset(cur, 0, sizeof(xmlAttr));
1786 cur->type = XML_ATTRIBUTE_NODE;
1787
1788 cur->parent = node;
Daniel Veillarda682b212001-06-07 19:59:42 +00001789 if (node != NULL) {
1790 doc = node->doc;
1791 cur->doc = doc;
1792 }
Owen Taylor3473f882001-02-23 17:55:21 +00001793 cur->ns = ns;
1794 cur->name = xmlStrdup(name);
1795 if (value != NULL) {
1796 xmlChar *buffer;
1797 xmlNodePtr tmp;
1798
Daniel Veillarda682b212001-06-07 19:59:42 +00001799 buffer = xmlEncodeEntitiesReentrant(doc, value);
1800 cur->children = xmlStringGetNodeList(doc, buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00001801 cur->last = NULL;
1802 tmp = cur->children;
1803 while (tmp != NULL) {
1804 tmp->parent = (xmlNodePtr) cur;
1805 if (tmp->next == NULL)
1806 cur->last = tmp;
1807 tmp = tmp->next;
1808 }
1809 xmlFree(buffer);
1810 }
1811
1812 /*
1813 * Add it at the end to preserve parsing order ...
1814 */
1815 if (node != NULL) {
1816 if (node->properties == NULL) {
1817 node->properties = cur;
1818 } else {
1819 xmlAttrPtr prev = node->properties;
1820
1821 while (prev->next != NULL) prev = prev->next;
1822 prev->next = cur;
1823 cur->prev = prev;
1824 }
1825 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001826
Daniel Veillarda880b122003-04-21 21:36:41 +00001827 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001828 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001829 return(cur);
1830}
1831
1832/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00001833 * xmlNewNsPropEatName:
1834 * @node: the holding node
1835 * @ns: the namespace
1836 * @name: the name of the attribute
1837 * @value: the value of the attribute
1838 *
1839 * Create a new property tagged with a namespace and carried by a node.
1840 * Returns a pointer to the attribute
1841 */
1842xmlAttrPtr
1843xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
1844 const xmlChar *value) {
1845 xmlAttrPtr cur;
1846 xmlDocPtr doc = NULL;
1847
1848 if (name == NULL) {
1849#ifdef DEBUG_TREE
1850 xmlGenericError(xmlGenericErrorContext,
1851 "xmlNewNsPropEatName : name == NULL\n");
1852#endif
1853 return(NULL);
1854 }
1855
1856 /*
1857 * Allocate a new property and fill the fields.
1858 */
1859 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1860 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001861 xmlTreeErrMemory("building attribute");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001862 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001863 return(NULL);
1864 }
1865 memset(cur, 0, sizeof(xmlAttr));
1866 cur->type = XML_ATTRIBUTE_NODE;
1867
1868 cur->parent = node;
1869 if (node != NULL) {
1870 doc = node->doc;
1871 cur->doc = doc;
1872 }
1873 cur->ns = ns;
1874 cur->name = name;
1875 if (value != NULL) {
1876 xmlChar *buffer;
1877 xmlNodePtr tmp;
1878
1879 buffer = xmlEncodeEntitiesReentrant(doc, value);
1880 cur->children = xmlStringGetNodeList(doc, buffer);
1881 cur->last = NULL;
1882 tmp = cur->children;
1883 while (tmp != NULL) {
1884 tmp->parent = (xmlNodePtr) cur;
1885 if (tmp->next == NULL)
1886 cur->last = tmp;
1887 tmp = tmp->next;
1888 }
1889 xmlFree(buffer);
1890 }
1891
1892 /*
1893 * Add it at the end to preserve parsing order ...
1894 */
1895 if (node != NULL) {
1896 if (node->properties == NULL) {
1897 node->properties = cur;
1898 } else {
1899 xmlAttrPtr prev = node->properties;
1900
1901 while (prev->next != NULL) prev = prev->next;
1902 prev->next = cur;
1903 cur->prev = prev;
1904 }
1905 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001906
Daniel Veillarda880b122003-04-21 21:36:41 +00001907 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00001908 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00001909 return(cur);
1910}
1911
1912/**
Owen Taylor3473f882001-02-23 17:55:21 +00001913 * xmlNewDocProp:
1914 * @doc: the document
1915 * @name: the name of the attribute
1916 * @value: the value of the attribute
1917 *
1918 * Create a new property carried by a document.
1919 * Returns a pointer to the attribute
1920 */
1921xmlAttrPtr
1922xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) {
1923 xmlAttrPtr cur;
1924
1925 if (name == NULL) {
1926#ifdef DEBUG_TREE
1927 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00001928 "xmlNewDocProp : name == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001929#endif
1930 return(NULL);
1931 }
1932
1933 /*
1934 * Allocate a new property and fill the fields.
1935 */
1936 cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
1937 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00001938 xmlTreeErrMemory("building attribute");
Owen Taylor3473f882001-02-23 17:55:21 +00001939 return(NULL);
1940 }
1941 memset(cur, 0, sizeof(xmlAttr));
1942 cur->type = XML_ATTRIBUTE_NODE;
1943
1944 cur->name = xmlStrdup(name);
1945 cur->doc = doc;
1946 if (value != NULL) {
1947 xmlNodePtr tmp;
1948
1949 cur->children = xmlStringGetNodeList(doc, value);
1950 cur->last = NULL;
1951
1952 tmp = cur->children;
1953 while (tmp != NULL) {
1954 tmp->parent = (xmlNodePtr) cur;
1955 if (tmp->next == NULL)
1956 cur->last = tmp;
1957 tmp = tmp->next;
1958 }
1959 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00001960
Daniel Veillarda880b122003-04-21 21:36:41 +00001961 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001962 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001963 return(cur);
1964}
1965
1966/**
1967 * xmlFreePropList:
1968 * @cur: the first property in the list
1969 *
1970 * Free a property and all its siblings, all the children are freed too.
1971 */
1972void
1973xmlFreePropList(xmlAttrPtr cur) {
1974 xmlAttrPtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001975 if (cur == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001976 while (cur != NULL) {
1977 next = cur->next;
1978 xmlFreeProp(cur);
1979 cur = next;
1980 }
1981}
1982
1983/**
1984 * xmlFreeProp:
1985 * @cur: an attribute
1986 *
1987 * Free one attribute, all the content is freed too
1988 */
1989void
1990xmlFreeProp(xmlAttrPtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00001991 xmlDictPtr dict = NULL;
1992 if (cur == NULL) return;
1993
1994 if (cur->doc != NULL) dict = cur->doc->dict;
Daniel Veillard5335dc52003-01-01 20:59:38 +00001995
Daniel Veillarda880b122003-04-21 21:36:41 +00001996 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00001997 xmlDeregisterNodeDefaultValue((xmlNodePtr)cur);
1998
Owen Taylor3473f882001-02-23 17:55:21 +00001999 /* Check for ID removal -> leading to invalid references ! */
Daniel Veillard76d66f42001-05-16 21:05:17 +00002000 if ((cur->parent != NULL) && (cur->parent->doc != NULL) &&
2001 ((cur->parent->doc->intSubset != NULL) ||
2002 (cur->parent->doc->extSubset != NULL))) {
2003 if (xmlIsID(cur->parent->doc, cur->parent, cur))
2004 xmlRemoveID(cur->parent->doc, cur);
2005 }
Owen Taylor3473f882001-02-23 17:55:21 +00002006 if (cur->children != NULL) xmlFreeNodeList(cur->children);
Daniel Veillarde96a2a42003-09-24 21:23:56 +00002007 DICT_FREE(cur->name)
Owen Taylor3473f882001-02-23 17:55:21 +00002008 xmlFree(cur);
2009}
2010
Daniel Veillard652327a2003-09-29 18:02:38 +00002011#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002012/**
2013 * xmlRemoveProp:
2014 * @cur: an attribute
2015 *
2016 * Unlink and free one attribute, all the content is freed too
2017 * Note this doesn't work for namespace definition attributes
2018 *
2019 * Returns 0 if success and -1 in case of error.
2020 */
2021int
2022xmlRemoveProp(xmlAttrPtr cur) {
2023 xmlAttrPtr tmp;
2024 if (cur == NULL) {
2025#ifdef DEBUG_TREE
2026 xmlGenericError(xmlGenericErrorContext,
2027 "xmlRemoveProp : cur == NULL\n");
2028#endif
2029 return(-1);
2030 }
2031 if (cur->parent == NULL) {
2032#ifdef DEBUG_TREE
2033 xmlGenericError(xmlGenericErrorContext,
2034 "xmlRemoveProp : cur->parent == NULL\n");
2035#endif
2036 return(-1);
2037 }
2038 tmp = cur->parent->properties;
2039 if (tmp == cur) {
2040 cur->parent->properties = cur->next;
2041 xmlFreeProp(cur);
2042 return(0);
2043 }
2044 while (tmp != NULL) {
2045 if (tmp->next == cur) {
2046 tmp->next = cur->next;
2047 if (tmp->next != NULL)
2048 tmp->next->prev = tmp;
2049 xmlFreeProp(cur);
2050 return(0);
2051 }
2052 tmp = tmp->next;
2053 }
2054#ifdef DEBUG_TREE
2055 xmlGenericError(xmlGenericErrorContext,
2056 "xmlRemoveProp : attribute not owned by its node\n");
2057#endif
2058 return(-1);
2059}
Daniel Veillard652327a2003-09-29 18:02:38 +00002060#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002061
2062/**
2063 * xmlNewPI:
2064 * @name: the processing instruction name
2065 * @content: the PI content
2066 *
2067 * Creation of a processing instruction element.
2068 * Returns a pointer to the new node object.
2069 */
2070xmlNodePtr
2071xmlNewPI(const xmlChar *name, const xmlChar *content) {
2072 xmlNodePtr cur;
2073
2074 if (name == NULL) {
2075#ifdef DEBUG_TREE
2076 xmlGenericError(xmlGenericErrorContext,
2077 "xmlNewPI : name == NULL\n");
2078#endif
2079 return(NULL);
2080 }
2081
2082 /*
2083 * Allocate a new node and fill the fields.
2084 */
2085 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2086 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002087 xmlTreeErrMemory("building PI");
Owen Taylor3473f882001-02-23 17:55:21 +00002088 return(NULL);
2089 }
2090 memset(cur, 0, sizeof(xmlNode));
2091 cur->type = XML_PI_NODE;
2092
2093 cur->name = xmlStrdup(name);
2094 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002095 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002096 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002097
Daniel Veillarda880b122003-04-21 21:36:41 +00002098 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002099 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002100 return(cur);
2101}
2102
2103/**
2104 * xmlNewNode:
2105 * @ns: namespace if any
2106 * @name: the node name
2107 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002108 * Creation of a new node element. @ns is optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002109 *
2110 * Returns a pointer to the new node object.
2111 */
2112xmlNodePtr
2113xmlNewNode(xmlNsPtr ns, const xmlChar *name) {
2114 xmlNodePtr cur;
2115
2116 if (name == NULL) {
2117#ifdef DEBUG_TREE
2118 xmlGenericError(xmlGenericErrorContext,
2119 "xmlNewNode : name == NULL\n");
2120#endif
2121 return(NULL);
2122 }
2123
2124 /*
2125 * Allocate a new node and fill the fields.
2126 */
2127 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2128 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002129 xmlTreeErrMemory("building node");
Owen Taylor3473f882001-02-23 17:55:21 +00002130 return(NULL);
2131 }
2132 memset(cur, 0, sizeof(xmlNode));
2133 cur->type = XML_ELEMENT_NODE;
2134
2135 cur->name = xmlStrdup(name);
2136 cur->ns = ns;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002137
Daniel Veillarda880b122003-04-21 21:36:41 +00002138 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002139 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002140 return(cur);
2141}
2142
2143/**
Daniel Veillard46de64e2002-05-29 08:21:33 +00002144 * xmlNewNodeEatName:
2145 * @ns: namespace if any
2146 * @name: the node name
2147 *
2148 * Creation of a new node element. @ns is optional (NULL).
2149 *
2150 * Returns a pointer to the new node object.
2151 */
2152xmlNodePtr
2153xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) {
2154 xmlNodePtr cur;
2155
2156 if (name == NULL) {
2157#ifdef DEBUG_TREE
2158 xmlGenericError(xmlGenericErrorContext,
2159 "xmlNewNode : name == NULL\n");
2160#endif
2161 return(NULL);
2162 }
2163
2164 /*
2165 * Allocate a new node and fill the fields.
2166 */
2167 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2168 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002169 xmlTreeErrMemory("building node");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002170 xmlFree(name);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002171 return(NULL);
2172 }
2173 memset(cur, 0, sizeof(xmlNode));
2174 cur->type = XML_ELEMENT_NODE;
2175
2176 cur->name = name;
2177 cur->ns = ns;
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002178
Daniel Veillarda880b122003-04-21 21:36:41 +00002179 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00002180 xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
Daniel Veillard46de64e2002-05-29 08:21:33 +00002181 return(cur);
2182}
2183
2184/**
Owen Taylor3473f882001-02-23 17:55:21 +00002185 * xmlNewDocNode:
2186 * @doc: the document
2187 * @ns: namespace if any
2188 * @name: the node name
2189 * @content: the XML text content if any
2190 *
2191 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002192 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002193 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2194 * references, but XML special chars need to be escaped first by using
2195 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2196 * need entities support.
2197 *
2198 * Returns a pointer to the new node object.
2199 */
2200xmlNodePtr
2201xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns,
2202 const xmlChar *name, const xmlChar *content) {
2203 xmlNodePtr cur;
2204
2205 cur = xmlNewNode(ns, name);
2206 if (cur != NULL) {
2207 cur->doc = doc;
2208 if (content != NULL) {
2209 cur->children = xmlStringGetNodeList(doc, content);
2210 UPDATE_LAST_CHILD_AND_PARENT(cur)
2211 }
2212 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002213
Owen Taylor3473f882001-02-23 17:55:21 +00002214 return(cur);
2215}
2216
Daniel Veillard46de64e2002-05-29 08:21:33 +00002217/**
2218 * xmlNewDocNodeEatName:
2219 * @doc: the document
2220 * @ns: namespace if any
2221 * @name: the node name
2222 * @content: the XML text content if any
2223 *
2224 * Creation of a new node element within a document. @ns and @content
2225 * are optional (NULL).
2226 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2227 * references, but XML special chars need to be escaped first by using
2228 * xmlEncodeEntitiesReentrant(). Use xmlNewDocRawNode() if you don't
2229 * need entities support.
2230 *
2231 * Returns a pointer to the new node object.
2232 */
2233xmlNodePtr
2234xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns,
2235 xmlChar *name, const xmlChar *content) {
2236 xmlNodePtr cur;
2237
2238 cur = xmlNewNodeEatName(ns, name);
2239 if (cur != NULL) {
2240 cur->doc = doc;
2241 if (content != NULL) {
2242 cur->children = xmlStringGetNodeList(doc, content);
2243 UPDATE_LAST_CHILD_AND_PARENT(cur)
2244 }
2245 }
2246 return(cur);
2247}
2248
Daniel Veillard652327a2003-09-29 18:02:38 +00002249#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002250/**
2251 * xmlNewDocRawNode:
2252 * @doc: the document
2253 * @ns: namespace if any
2254 * @name: the node name
2255 * @content: the text content if any
2256 *
2257 * Creation of a new node element within a document. @ns and @content
Daniel Veillardd1640922001-12-17 15:30:10 +00002258 * are optional (NULL).
Owen Taylor3473f882001-02-23 17:55:21 +00002259 *
2260 * Returns a pointer to the new node object.
2261 */
2262xmlNodePtr
2263xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns,
2264 const xmlChar *name, const xmlChar *content) {
2265 xmlNodePtr cur;
2266
2267 cur = xmlNewNode(ns, name);
2268 if (cur != NULL) {
2269 cur->doc = doc;
2270 if (content != NULL) {
2271 cur->children = xmlNewDocText(doc, content);
2272 UPDATE_LAST_CHILD_AND_PARENT(cur)
2273 }
2274 }
2275 return(cur);
2276}
2277
2278/**
2279 * xmlNewDocFragment:
2280 * @doc: the document owning the fragment
2281 *
2282 * Creation of a new Fragment node.
2283 * Returns a pointer to the new node object.
2284 */
2285xmlNodePtr
2286xmlNewDocFragment(xmlDocPtr doc) {
2287 xmlNodePtr cur;
2288
2289 /*
2290 * Allocate a new DocumentFragment node and fill the fields.
2291 */
2292 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2293 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002294 xmlTreeErrMemory("building fragment");
Owen Taylor3473f882001-02-23 17:55:21 +00002295 return(NULL);
2296 }
2297 memset(cur, 0, sizeof(xmlNode));
2298 cur->type = XML_DOCUMENT_FRAG_NODE;
2299
2300 cur->doc = doc;
Daniel Veillard5335dc52003-01-01 20:59:38 +00002301
Daniel Veillarda880b122003-04-21 21:36:41 +00002302 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002303 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002304 return(cur);
2305}
Daniel Veillard652327a2003-09-29 18:02:38 +00002306#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002307
2308/**
2309 * xmlNewText:
2310 * @content: the text content
2311 *
2312 * Creation of a new text node.
2313 * Returns a pointer to the new node object.
2314 */
2315xmlNodePtr
2316xmlNewText(const xmlChar *content) {
2317 xmlNodePtr cur;
2318
2319 /*
2320 * Allocate a new node and fill the fields.
2321 */
2322 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2323 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002324 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002325 return(NULL);
2326 }
2327 memset(cur, 0, sizeof(xmlNode));
2328 cur->type = XML_TEXT_NODE;
2329
2330 cur->name = xmlStringText;
2331 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002332 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002333 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002334
Daniel Veillarda880b122003-04-21 21:36:41 +00002335 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002336 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002337 return(cur);
2338}
2339
Daniel Veillard652327a2003-09-29 18:02:38 +00002340#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002341/**
2342 * xmlNewTextChild:
2343 * @parent: the parent node
2344 * @ns: a namespace if any
2345 * @name: the name of the child
2346 * @content: the text content of the child if any.
2347 *
2348 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00002349 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002350 * a child TEXT node will be created containing the string content.
2351 *
2352 * Returns a pointer to the new node object.
2353 */
2354xmlNodePtr
2355xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns,
2356 const xmlChar *name, const xmlChar *content) {
2357 xmlNodePtr cur, prev;
2358
2359 if (parent == NULL) {
2360#ifdef DEBUG_TREE
2361 xmlGenericError(xmlGenericErrorContext,
2362 "xmlNewTextChild : parent == NULL\n");
2363#endif
2364 return(NULL);
2365 }
2366
2367 if (name == NULL) {
2368#ifdef DEBUG_TREE
2369 xmlGenericError(xmlGenericErrorContext,
2370 "xmlNewTextChild : name == NULL\n");
2371#endif
2372 return(NULL);
2373 }
2374
2375 /*
2376 * Allocate a new node
2377 */
2378 if (ns == NULL)
2379 cur = xmlNewDocRawNode(parent->doc, parent->ns, name, content);
2380 else
2381 cur = xmlNewDocRawNode(parent->doc, ns, name, content);
2382 if (cur == NULL) return(NULL);
2383
2384 /*
2385 * add the new element at the end of the children list.
2386 */
2387 cur->type = XML_ELEMENT_NODE;
2388 cur->parent = parent;
2389 cur->doc = parent->doc;
2390 if (parent->children == NULL) {
2391 parent->children = cur;
2392 parent->last = cur;
2393 } else {
2394 prev = parent->last;
2395 prev->next = cur;
2396 cur->prev = prev;
2397 parent->last = cur;
2398 }
2399
2400 return(cur);
2401}
Daniel Veillard652327a2003-09-29 18:02:38 +00002402#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002403
2404/**
2405 * xmlNewCharRef:
2406 * @doc: the document
2407 * @name: the char ref string, starting with # or "&# ... ;"
2408 *
2409 * Creation of a new character reference node.
2410 * Returns a pointer to the new node object.
2411 */
2412xmlNodePtr
2413xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) {
2414 xmlNodePtr cur;
2415
2416 /*
2417 * Allocate a new node and fill the fields.
2418 */
2419 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2420 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002421 xmlTreeErrMemory("building character reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002422 return(NULL);
2423 }
2424 memset(cur, 0, sizeof(xmlNode));
2425 cur->type = XML_ENTITY_REF_NODE;
2426
2427 cur->doc = doc;
2428 if (name[0] == '&') {
2429 int len;
2430 name++;
2431 len = xmlStrlen(name);
2432 if (name[len - 1] == ';')
2433 cur->name = xmlStrndup(name, len - 1);
2434 else
2435 cur->name = xmlStrndup(name, len);
2436 } else
2437 cur->name = xmlStrdup(name);
Daniel Veillard5335dc52003-01-01 20:59:38 +00002438
Daniel Veillarda880b122003-04-21 21:36:41 +00002439 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002440 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002441 return(cur);
2442}
2443
2444/**
2445 * xmlNewReference:
2446 * @doc: the document
2447 * @name: the reference name, or the reference string with & and ;
2448 *
2449 * Creation of a new reference node.
2450 * Returns a pointer to the new node object.
2451 */
2452xmlNodePtr
2453xmlNewReference(xmlDocPtr doc, const xmlChar *name) {
2454 xmlNodePtr cur;
2455 xmlEntityPtr ent;
2456
2457 /*
2458 * Allocate a new node and fill the fields.
2459 */
2460 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2461 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002462 xmlTreeErrMemory("building reference");
Owen Taylor3473f882001-02-23 17:55:21 +00002463 return(NULL);
2464 }
2465 memset(cur, 0, sizeof(xmlNode));
2466 cur->type = XML_ENTITY_REF_NODE;
2467
2468 cur->doc = doc;
2469 if (name[0] == '&') {
2470 int len;
2471 name++;
2472 len = xmlStrlen(name);
2473 if (name[len - 1] == ';')
2474 cur->name = xmlStrndup(name, len - 1);
2475 else
2476 cur->name = xmlStrndup(name, len);
2477 } else
2478 cur->name = xmlStrdup(name);
2479
2480 ent = xmlGetDocEntity(doc, cur->name);
2481 if (ent != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002482 cur->content = ent->content;
Owen Taylor3473f882001-02-23 17:55:21 +00002483 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002484 * The parent pointer in entity is a DTD pointer and thus is NOT
Owen Taylor3473f882001-02-23 17:55:21 +00002485 * updated. Not sure if this is 100% correct.
2486 * -George
2487 */
2488 cur->children = (xmlNodePtr) ent;
2489 cur->last = (xmlNodePtr) ent;
2490 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002491
Daniel Veillarda880b122003-04-21 21:36:41 +00002492 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002493 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002494 return(cur);
2495}
2496
2497/**
2498 * xmlNewDocText:
2499 * @doc: the document
2500 * @content: the text content
2501 *
2502 * Creation of a new text node within a document.
2503 * Returns a pointer to the new node object.
2504 */
2505xmlNodePtr
2506xmlNewDocText(xmlDocPtr doc, const xmlChar *content) {
2507 xmlNodePtr cur;
2508
2509 cur = xmlNewText(content);
2510 if (cur != NULL) cur->doc = doc;
2511 return(cur);
2512}
2513
2514/**
2515 * xmlNewTextLen:
2516 * @content: the text content
2517 * @len: the text len.
2518 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002519 * Creation of a new text node with an extra parameter for the content's length
Owen Taylor3473f882001-02-23 17:55:21 +00002520 * Returns a pointer to the new node object.
2521 */
2522xmlNodePtr
2523xmlNewTextLen(const xmlChar *content, int len) {
2524 xmlNodePtr cur;
2525
2526 /*
2527 * Allocate a new node and fill the fields.
2528 */
2529 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2530 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002531 xmlTreeErrMemory("building text");
Owen Taylor3473f882001-02-23 17:55:21 +00002532 return(NULL);
2533 }
2534 memset(cur, 0, sizeof(xmlNode));
2535 cur->type = XML_TEXT_NODE;
2536
2537 cur->name = xmlStringText;
2538 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002539 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002540 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002541
Daniel Veillarda880b122003-04-21 21:36:41 +00002542 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002543 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002544 return(cur);
2545}
2546
2547/**
2548 * xmlNewDocTextLen:
2549 * @doc: the document
2550 * @content: the text content
2551 * @len: the text len.
2552 *
Daniel Veillard60087f32001-10-10 09:45:09 +00002553 * Creation of a new text node with an extra content length parameter. The
Owen Taylor3473f882001-02-23 17:55:21 +00002554 * text node pertain to a given document.
2555 * Returns a pointer to the new node object.
2556 */
2557xmlNodePtr
2558xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) {
2559 xmlNodePtr cur;
2560
2561 cur = xmlNewTextLen(content, len);
2562 if (cur != NULL) cur->doc = doc;
2563 return(cur);
2564}
2565
2566/**
2567 * xmlNewComment:
2568 * @content: the comment content
2569 *
2570 * Creation of a new node containing a comment.
2571 * Returns a pointer to the new node object.
2572 */
2573xmlNodePtr
2574xmlNewComment(const xmlChar *content) {
2575 xmlNodePtr cur;
2576
2577 /*
2578 * Allocate a new node and fill the fields.
2579 */
2580 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2581 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002582 xmlTreeErrMemory("building comment");
Owen Taylor3473f882001-02-23 17:55:21 +00002583 return(NULL);
2584 }
2585 memset(cur, 0, sizeof(xmlNode));
2586 cur->type = XML_COMMENT_NODE;
2587
2588 cur->name = xmlStringComment;
2589 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002590 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00002591 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002592
Daniel Veillarda880b122003-04-21 21:36:41 +00002593 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002594 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002595 return(cur);
2596}
2597
2598/**
2599 * xmlNewCDataBlock:
2600 * @doc: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00002601 * @content: the CDATA block content content
Owen Taylor3473f882001-02-23 17:55:21 +00002602 * @len: the length of the block
2603 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002604 * Creation of a new node containing a CDATA block.
Owen Taylor3473f882001-02-23 17:55:21 +00002605 * Returns a pointer to the new node object.
2606 */
2607xmlNodePtr
2608xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) {
2609 xmlNodePtr cur;
2610
2611 /*
2612 * Allocate a new node and fill the fields.
2613 */
2614 cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
2615 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00002616 xmlTreeErrMemory("building CDATA");
Owen Taylor3473f882001-02-23 17:55:21 +00002617 return(NULL);
2618 }
2619 memset(cur, 0, sizeof(xmlNode));
2620 cur->type = XML_CDATA_SECTION_NODE;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002621 cur->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +00002622
2623 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002624 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00002625 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00002626
Daniel Veillarda880b122003-04-21 21:36:41 +00002627 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00002628 xmlRegisterNodeDefaultValue(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00002629 return(cur);
2630}
2631
2632/**
2633 * xmlNewDocComment:
2634 * @doc: the document
2635 * @content: the comment content
2636 *
Daniel Veillardd1640922001-12-17 15:30:10 +00002637 * Creation of a new node containing a comment within a document.
Owen Taylor3473f882001-02-23 17:55:21 +00002638 * Returns a pointer to the new node object.
2639 */
2640xmlNodePtr
2641xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) {
2642 xmlNodePtr cur;
2643
2644 cur = xmlNewComment(content);
2645 if (cur != NULL) cur->doc = doc;
2646 return(cur);
2647}
2648
2649/**
2650 * xmlSetTreeDoc:
2651 * @tree: the top element
2652 * @doc: the document
2653 *
2654 * update all nodes under the tree to point to the right document
2655 */
2656void
2657xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
Daniel Veillard19e96c32001-07-09 10:32:59 +00002658 xmlAttrPtr prop;
2659
Owen Taylor3473f882001-02-23 17:55:21 +00002660 if (tree == NULL)
2661 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002662 if (tree->doc != doc) {
Daniel Veillard36065812002-01-24 15:02:46 +00002663 if(tree->type == XML_ELEMENT_NODE) {
2664 prop = tree->properties;
2665 while (prop != NULL) {
2666 prop->doc = doc;
2667 xmlSetListDoc(prop->children, doc);
2668 prop = prop->next;
2669 }
Daniel Veillard19e96c32001-07-09 10:32:59 +00002670 }
Owen Taylor3473f882001-02-23 17:55:21 +00002671 if (tree->children != NULL)
2672 xmlSetListDoc(tree->children, doc);
2673 tree->doc = doc;
2674 }
2675}
2676
2677/**
2678 * xmlSetListDoc:
Daniel Veillardd1640922001-12-17 15:30:10 +00002679 * @list: the first element
Owen Taylor3473f882001-02-23 17:55:21 +00002680 * @doc: the document
2681 *
2682 * update all nodes in the list to point to the right document
2683 */
2684void
2685xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) {
2686 xmlNodePtr cur;
2687
2688 if (list == NULL)
2689 return;
2690 cur = list;
2691 while (cur != NULL) {
2692 if (cur->doc != doc)
2693 xmlSetTreeDoc(cur, doc);
2694 cur = cur->next;
2695 }
2696}
2697
Daniel Veillard652327a2003-09-29 18:02:38 +00002698#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002699/**
2700 * xmlNewChild:
2701 * @parent: the parent node
2702 * @ns: a namespace if any
2703 * @name: the name of the child
2704 * @content: the XML content of the child if any.
2705 *
2706 * Creation of a new child element, added at the end of @parent children list.
Daniel Veillardd1640922001-12-17 15:30:10 +00002707 * @ns and @content parameters are optional (NULL). If content is non NULL,
Owen Taylor3473f882001-02-23 17:55:21 +00002708 * a child list containing the TEXTs and ENTITY_REFs node will be created.
2709 * NOTE: @content is supposed to be a piece of XML CDATA, so it allow entities
2710 * references, but XML special chars need to be escaped first by using
2711 * xmlEncodeEntitiesReentrant(). Use xmlNewTextChild() if entities
2712 * support is not needed.
2713 *
2714 * Returns a pointer to the new node object.
2715 */
2716xmlNodePtr
2717xmlNewChild(xmlNodePtr parent, xmlNsPtr ns,
2718 const xmlChar *name, const xmlChar *content) {
2719 xmlNodePtr cur, prev;
2720
2721 if (parent == NULL) {
2722#ifdef DEBUG_TREE
2723 xmlGenericError(xmlGenericErrorContext,
2724 "xmlNewChild : parent == NULL\n");
2725#endif
2726 return(NULL);
2727 }
2728
2729 if (name == NULL) {
2730#ifdef DEBUG_TREE
2731 xmlGenericError(xmlGenericErrorContext,
2732 "xmlNewChild : name == NULL\n");
2733#endif
2734 return(NULL);
2735 }
2736
2737 /*
2738 * Allocate a new node
2739 */
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002740 if (parent->type == XML_ELEMENT_NODE) {
2741 if (ns == NULL)
2742 cur = xmlNewDocNode(parent->doc, parent->ns, name, content);
2743 else
2744 cur = xmlNewDocNode(parent->doc, ns, name, content);
2745 } else if ((parent->type == XML_DOCUMENT_NODE) ||
2746 (parent->type == XML_HTML_DOCUMENT_NODE)) {
2747 if (ns == NULL)
2748 cur = xmlNewDocNode((xmlDocPtr) parent, NULL, name, content);
2749 else
2750 cur = xmlNewDocNode((xmlDocPtr) parent, ns, name, content);
Daniel Veillard7e3f1402002-10-28 18:52:57 +00002751 } else if (parent->type == XML_DOCUMENT_FRAG_NODE) {
2752 cur = xmlNewDocNode( parent->doc, ns, name, content);
Daniel Veillard36eea2d2002-02-04 00:17:01 +00002753 } else {
2754 return(NULL);
2755 }
Owen Taylor3473f882001-02-23 17:55:21 +00002756 if (cur == NULL) return(NULL);
2757
2758 /*
2759 * add the new element at the end of the children list.
2760 */
2761 cur->type = XML_ELEMENT_NODE;
2762 cur->parent = parent;
2763 cur->doc = parent->doc;
2764 if (parent->children == NULL) {
2765 parent->children = cur;
2766 parent->last = cur;
2767 } else {
2768 prev = parent->last;
2769 prev->next = cur;
2770 cur->prev = prev;
2771 parent->last = cur;
2772 }
2773
2774 return(cur);
2775}
Daniel Veillard652327a2003-09-29 18:02:38 +00002776#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002777
2778/**
2779 * xmlAddNextSibling:
2780 * @cur: the child node
2781 * @elem: the new node
2782 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002783 * Add a new node @elem as the next sibling of @cur
2784 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002785 * first unlinked from its existing context.
2786 * As a result of text merging @elem may be freed.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002787 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2788 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002789 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002790 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002791 */
2792xmlNodePtr
2793xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) {
2794 if (cur == NULL) {
2795#ifdef DEBUG_TREE
2796 xmlGenericError(xmlGenericErrorContext,
2797 "xmlAddNextSibling : cur == NULL\n");
2798#endif
2799 return(NULL);
2800 }
2801 if (elem == NULL) {
2802#ifdef DEBUG_TREE
2803 xmlGenericError(xmlGenericErrorContext,
2804 "xmlAddNextSibling : elem == NULL\n");
2805#endif
2806 return(NULL);
2807 }
2808
2809 xmlUnlinkNode(elem);
2810
2811 if (elem->type == XML_TEXT_NODE) {
2812 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002813 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002814 xmlFreeNode(elem);
2815 return(cur);
2816 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002817 if ((cur->next != NULL) && (cur->next->type == XML_TEXT_NODE) &&
2818 (cur->name == cur->next->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002819 xmlChar *tmp;
2820
2821 tmp = xmlStrdup(elem->content);
2822 tmp = xmlStrcat(tmp, cur->next->content);
2823 xmlNodeSetContent(cur->next, tmp);
2824 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002825 xmlFreeNode(elem);
2826 return(cur->next);
2827 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002828 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2829 /* check if an attribute with the same name exists */
2830 xmlAttrPtr attr;
2831
2832 if (elem->ns == NULL)
2833 attr = xmlHasProp(cur->parent, elem->name);
2834 else
2835 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2836 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2837 /* different instance, destroy it (attributes must be unique) */
2838 xmlFreeProp(attr);
2839 }
Owen Taylor3473f882001-02-23 17:55:21 +00002840 }
2841
2842 if (elem->doc != cur->doc) {
2843 xmlSetTreeDoc(elem, cur->doc);
2844 }
2845 elem->parent = cur->parent;
2846 elem->prev = cur;
2847 elem->next = cur->next;
2848 cur->next = elem;
2849 if (elem->next != NULL)
2850 elem->next->prev = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002851 if ((elem->parent != NULL) && (elem->parent->last == cur) && (elem->type != XML_ATTRIBUTE_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00002852 elem->parent->last = elem;
2853 return(elem);
2854}
2855
Daniel Veillard652327a2003-09-29 18:02:38 +00002856#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002857/**
2858 * xmlAddPrevSibling:
2859 * @cur: the child node
2860 * @elem: the new node
2861 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002862 * Add a new node @elem as the previous sibling of @cur
Owen Taylor3473f882001-02-23 17:55:21 +00002863 * merging adjacent TEXT nodes (@elem may be freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002864 * If the new node was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00002865 * first unlinked from its existing context.
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002866 * If the new node is ATTRIBUTE, it is added into properties instead of children.
2867 * If there is an attribute with equal name, it is first destroyed.
Owen Taylor3473f882001-02-23 17:55:21 +00002868 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002869 * Returns the new node or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002870 */
2871xmlNodePtr
2872xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) {
2873 if (cur == NULL) {
2874#ifdef DEBUG_TREE
2875 xmlGenericError(xmlGenericErrorContext,
2876 "xmlAddPrevSibling : cur == NULL\n");
2877#endif
2878 return(NULL);
2879 }
2880 if (elem == NULL) {
2881#ifdef DEBUG_TREE
2882 xmlGenericError(xmlGenericErrorContext,
2883 "xmlAddPrevSibling : elem == NULL\n");
2884#endif
2885 return(NULL);
2886 }
2887
2888 xmlUnlinkNode(elem);
2889
2890 if (elem->type == XML_TEXT_NODE) {
2891 if (cur->type == XML_TEXT_NODE) {
Owen Taylor3473f882001-02-23 17:55:21 +00002892 xmlChar *tmp;
2893
2894 tmp = xmlStrdup(elem->content);
2895 tmp = xmlStrcat(tmp, cur->content);
2896 xmlNodeSetContent(cur, tmp);
2897 xmlFree(tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002898 xmlFreeNode(elem);
2899 return(cur);
2900 }
Daniel Veillard9e1c72d2001-08-31 20:03:19 +00002901 if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE) &&
2902 (cur->name == cur->prev->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002903 xmlNodeAddContent(cur->prev, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002904 xmlFreeNode(elem);
2905 return(cur->prev);
2906 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002907 } else if (elem->type == XML_ATTRIBUTE_NODE) {
2908 /* check if an attribute with the same name exists */
2909 xmlAttrPtr attr;
2910
2911 if (elem->ns == NULL)
2912 attr = xmlHasProp(cur->parent, elem->name);
2913 else
2914 attr = xmlHasNsProp(cur->parent, elem->name, elem->ns->href);
2915 if ((attr != NULL) && (attr != (xmlAttrPtr) elem)) {
2916 /* different instance, destroy it (attributes must be unique) */
2917 xmlFreeProp(attr);
2918 }
Owen Taylor3473f882001-02-23 17:55:21 +00002919 }
2920
2921 if (elem->doc != cur->doc) {
2922 xmlSetTreeDoc(elem, cur->doc);
2923 }
2924 elem->parent = cur->parent;
2925 elem->next = cur;
2926 elem->prev = cur->prev;
2927 cur->prev = elem;
2928 if (elem->prev != NULL)
2929 elem->prev->next = elem;
Daniel Veillardbd227ae2002-01-24 16:05:41 +00002930 if (elem->parent != NULL) {
2931 if (elem->type == XML_ATTRIBUTE_NODE) {
2932 if (elem->parent->properties == (xmlAttrPtr) cur) {
2933 elem->parent->properties = (xmlAttrPtr) elem;
2934 }
2935 } else {
2936 if (elem->parent->children == cur) {
2937 elem->parent->children = elem;
2938 }
2939 }
2940 }
Owen Taylor3473f882001-02-23 17:55:21 +00002941 return(elem);
2942}
Daniel Veillard652327a2003-09-29 18:02:38 +00002943#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002944
2945/**
2946 * xmlAddSibling:
2947 * @cur: the child node
2948 * @elem: the new node
2949 *
2950 * Add a new element @elem to the list of siblings of @cur
2951 * merging adjacent TEXT nodes (@elem may be freed)
2952 * If the new element was already inserted in a document it is
2953 * first unlinked from its existing context.
2954 *
2955 * Returns the new element or NULL in case of error.
2956 */
2957xmlNodePtr
2958xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) {
2959 xmlNodePtr parent;
2960
2961 if (cur == NULL) {
2962#ifdef DEBUG_TREE
2963 xmlGenericError(xmlGenericErrorContext,
2964 "xmlAddSibling : cur == NULL\n");
2965#endif
2966 return(NULL);
2967 }
2968
2969 if (elem == NULL) {
2970#ifdef DEBUG_TREE
2971 xmlGenericError(xmlGenericErrorContext,
2972 "xmlAddSibling : elem == NULL\n");
2973#endif
2974 return(NULL);
2975 }
2976
2977 /*
2978 * Constant time is we can rely on the ->parent->last to find
2979 * the last sibling.
2980 */
2981 if ((cur->parent != NULL) &&
2982 (cur->parent->children != NULL) &&
2983 (cur->parent->last != NULL) &&
2984 (cur->parent->last->next == NULL)) {
2985 cur = cur->parent->last;
2986 } else {
2987 while (cur->next != NULL) cur = cur->next;
2988 }
2989
2990 xmlUnlinkNode(elem);
2991
2992 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002993 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002994 xmlFreeNode(elem);
2995 return(cur);
2996 }
2997
2998 if (elem->doc != cur->doc) {
2999 xmlSetTreeDoc(elem, cur->doc);
3000 }
3001 parent = cur->parent;
3002 elem->prev = cur;
3003 elem->next = NULL;
3004 elem->parent = parent;
3005 cur->next = elem;
3006 if (parent != NULL)
3007 parent->last = elem;
3008
3009 return(elem);
3010}
3011
3012/**
3013 * xmlAddChildList:
3014 * @parent: the parent node
3015 * @cur: the first node in the list
3016 *
3017 * Add a list of node at the end of the child list of the parent
3018 * merging adjacent TEXT nodes (@cur may be freed)
3019 *
3020 * Returns the last child or NULL in case of error.
3021 */
3022xmlNodePtr
3023xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3024 xmlNodePtr prev;
3025
3026 if (parent == NULL) {
3027#ifdef DEBUG_TREE
3028 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003029 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003030#endif
3031 return(NULL);
3032 }
3033
3034 if (cur == NULL) {
3035#ifdef DEBUG_TREE
3036 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003037 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003038#endif
3039 return(NULL);
3040 }
3041
3042 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3043 (cur->doc != parent->doc)) {
3044#ifdef DEBUG_TREE
3045 xmlGenericError(xmlGenericErrorContext,
3046 "Elements moved to a different document\n");
3047#endif
3048 }
3049
3050 /*
3051 * add the first element at the end of the children list.
3052 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003053
Owen Taylor3473f882001-02-23 17:55:21 +00003054 if (parent->children == NULL) {
3055 parent->children = cur;
3056 } else {
3057 /*
3058 * If cur and parent->last both are TEXT nodes, then merge them.
3059 */
3060 if ((cur->type == XML_TEXT_NODE) &&
3061 (parent->last->type == XML_TEXT_NODE) &&
3062 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003063 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003064 /*
3065 * if it's the only child, nothing more to be done.
3066 */
3067 if (cur->next == NULL) {
3068 xmlFreeNode(cur);
3069 return(parent->last);
3070 }
3071 prev = cur;
3072 cur = cur->next;
3073 xmlFreeNode(prev);
3074 }
3075 prev = parent->last;
3076 prev->next = cur;
3077 cur->prev = prev;
3078 }
3079 while (cur->next != NULL) {
3080 cur->parent = parent;
3081 if (cur->doc != parent->doc) {
3082 xmlSetTreeDoc(cur, parent->doc);
3083 }
3084 cur = cur->next;
3085 }
3086 cur->parent = parent;
3087 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3088 parent->last = cur;
3089
3090 return(cur);
3091}
3092
3093/**
3094 * xmlAddChild:
3095 * @parent: the parent node
3096 * @cur: the child node
3097 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003098 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003099 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003100 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3101 * If there is an attribute with equal name, it is first destroyed.
3102 *
Owen Taylor3473f882001-02-23 17:55:21 +00003103 * Returns the child or NULL in case of error.
3104 */
3105xmlNodePtr
3106xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3107 xmlNodePtr prev;
3108
3109 if (parent == NULL) {
3110#ifdef DEBUG_TREE
3111 xmlGenericError(xmlGenericErrorContext,
3112 "xmlAddChild : parent == NULL\n");
3113#endif
3114 return(NULL);
3115 }
3116
3117 if (cur == NULL) {
3118#ifdef DEBUG_TREE
3119 xmlGenericError(xmlGenericErrorContext,
3120 "xmlAddChild : child == NULL\n");
3121#endif
3122 return(NULL);
3123 }
3124
Owen Taylor3473f882001-02-23 17:55:21 +00003125 /*
3126 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003127 * cur is then freed.
3128 */
3129 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003130 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003131 (parent->content != NULL) &&
3132 (parent != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003133 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003134 xmlFreeNode(cur);
3135 return(parent);
3136 }
3137 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003138 (parent->last->name == cur->name) &&
3139 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003140 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003141 xmlFreeNode(cur);
3142 return(parent->last);
3143 }
3144 }
3145
3146 /*
3147 * add the new element at the end of the children list.
3148 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003149 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003150 cur->parent = parent;
3151 if (cur->doc != parent->doc) {
3152 xmlSetTreeDoc(cur, parent->doc);
3153 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003154 /* this check prevents a loop on tree-traversions if a developer
3155 * tries to add a node to its parent multiple times
3156 */
3157 if (prev == parent)
3158 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003159
3160 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003161 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003162 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003163 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003164 (parent->content != NULL) &&
3165 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003166 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003167 xmlFreeNode(cur);
3168 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003169 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003170 if (cur->type == XML_ATTRIBUTE_NODE) {
3171 if (parent->properties == NULL) {
3172 parent->properties = (xmlAttrPtr) cur;
3173 } else {
3174 /* check if an attribute with the same name exists */
3175 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003176
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003177 if (cur->ns == NULL)
3178 lastattr = xmlHasProp(parent, cur->name);
3179 else
3180 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3181 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
3182 /* different instance, destroy it (attributes must be unique) */
3183 xmlFreeProp(lastattr);
3184 }
3185 /* find the end */
3186 lastattr = parent->properties;
3187 while (lastattr->next != NULL) {
3188 lastattr = lastattr->next;
3189 }
3190 lastattr->next = (xmlAttrPtr) cur;
3191 ((xmlAttrPtr) cur)->prev = lastattr;
3192 }
3193 } else {
3194 if (parent->children == NULL) {
3195 parent->children = cur;
3196 parent->last = cur;
3197 } else {
3198 prev = parent->last;
3199 prev->next = cur;
3200 cur->prev = prev;
3201 parent->last = cur;
3202 }
3203 }
Owen Taylor3473f882001-02-23 17:55:21 +00003204 return(cur);
3205}
3206
3207/**
3208 * xmlGetLastChild:
3209 * @parent: the parent node
3210 *
3211 * Search the last child of a node.
3212 * Returns the last child or NULL if none.
3213 */
3214xmlNodePtr
3215xmlGetLastChild(xmlNodePtr parent) {
3216 if (parent == NULL) {
3217#ifdef DEBUG_TREE
3218 xmlGenericError(xmlGenericErrorContext,
3219 "xmlGetLastChild : parent == NULL\n");
3220#endif
3221 return(NULL);
3222 }
3223 return(parent->last);
3224}
3225
3226/**
3227 * xmlFreeNodeList:
3228 * @cur: the first node in the list
3229 *
3230 * Free a node and all its siblings, this is a recursive behaviour, all
3231 * the children are freed too.
3232 */
3233void
3234xmlFreeNodeList(xmlNodePtr cur) {
3235 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003236 xmlDictPtr dict = NULL;
3237
3238 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003239 if (cur->type == XML_NAMESPACE_DECL) {
3240 xmlFreeNsList((xmlNsPtr) cur);
3241 return;
3242 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003243 if ((cur->type == XML_DOCUMENT_NODE) ||
3244#ifdef LIBXML_DOCB_ENABLED
3245 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003246#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003247 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003248 xmlFreeDoc((xmlDocPtr) cur);
3249 return;
3250 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003251 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003252 while (cur != NULL) {
3253 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003254 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003255
Daniel Veillarda880b122003-04-21 21:36:41 +00003256 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003257 xmlDeregisterNodeDefaultValue(cur);
3258
Daniel Veillard02141ea2001-04-30 11:46:40 +00003259 if ((cur->children != NULL) &&
3260 (cur->type != XML_ENTITY_REF_NODE))
3261 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003262 if (((cur->type == XML_ELEMENT_NODE) ||
3263 (cur->type == XML_XINCLUDE_START) ||
3264 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003265 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003266 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003267 if ((cur->type != XML_ELEMENT_NODE) &&
3268 (cur->type != XML_XINCLUDE_START) &&
3269 (cur->type != XML_XINCLUDE_END) &&
3270 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003271 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003272 }
3273 if (((cur->type == XML_ELEMENT_NODE) ||
3274 (cur->type == XML_XINCLUDE_START) ||
3275 (cur->type == XML_XINCLUDE_END)) &&
3276 (cur->nsDef != NULL))
3277 xmlFreeNsList(cur->nsDef);
3278
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003279 /*
3280 * When a node is a text node or a comment, it uses a global static
3281 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003282 * Otherwise the node name might come from the document's
3283 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003284 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003285 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003286 (cur->type != XML_TEXT_NODE) &&
3287 (cur->type != XML_COMMENT_NODE))
3288 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003289 xmlFree(cur);
3290 }
Owen Taylor3473f882001-02-23 17:55:21 +00003291 cur = next;
3292 }
3293}
3294
3295/**
3296 * xmlFreeNode:
3297 * @cur: the node
3298 *
3299 * Free a node, this is a recursive behaviour, all the children are freed too.
3300 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3301 */
3302void
3303xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003304 xmlDictPtr dict = NULL;
3305
3306 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003307
Daniel Veillard02141ea2001-04-30 11:46:40 +00003308 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003309 if (cur->type == XML_DTD_NODE) {
3310 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003311 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003312 }
3313 if (cur->type == XML_NAMESPACE_DECL) {
3314 xmlFreeNs((xmlNsPtr) cur);
3315 return;
3316 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003317 if (cur->type == XML_ATTRIBUTE_NODE) {
3318 xmlFreeProp((xmlAttrPtr) cur);
3319 return;
3320 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003321
Daniel Veillarda880b122003-04-21 21:36:41 +00003322 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003323 xmlDeregisterNodeDefaultValue(cur);
3324
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003325 if (cur->doc != NULL) dict = cur->doc->dict;
3326
Owen Taylor3473f882001-02-23 17:55:21 +00003327 if ((cur->children != NULL) &&
3328 (cur->type != XML_ENTITY_REF_NODE))
3329 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003330 if (((cur->type == XML_ELEMENT_NODE) ||
3331 (cur->type == XML_XINCLUDE_START) ||
3332 (cur->type == XML_XINCLUDE_END)) &&
3333 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003334 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003335 if ((cur->type != XML_ELEMENT_NODE) &&
3336 (cur->content != NULL) &&
3337 (cur->type != XML_ENTITY_REF_NODE) &&
3338 (cur->type != XML_XINCLUDE_END) &&
3339 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003340 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003341 }
3342
Daniel Veillardacd370f2001-06-09 17:17:51 +00003343 /*
3344 * When a node is a text node or a comment, it uses a global static
3345 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003346 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003347 */
Owen Taylor3473f882001-02-23 17:55:21 +00003348 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003349 (cur->type != XML_TEXT_NODE) &&
3350 (cur->type != XML_COMMENT_NODE))
3351 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003352
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003353 if (((cur->type == XML_ELEMENT_NODE) ||
3354 (cur->type == XML_XINCLUDE_START) ||
3355 (cur->type == XML_XINCLUDE_END)) &&
3356 (cur->nsDef != NULL))
3357 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003358 xmlFree(cur);
3359}
3360
3361/**
3362 * xmlUnlinkNode:
3363 * @cur: the node
3364 *
3365 * Unlink a node from it's current context, the node is not freed
3366 */
3367void
3368xmlUnlinkNode(xmlNodePtr cur) {
3369 if (cur == NULL) {
3370#ifdef DEBUG_TREE
3371 xmlGenericError(xmlGenericErrorContext,
3372 "xmlUnlinkNode : node == NULL\n");
3373#endif
3374 return;
3375 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003376 if (cur->type == XML_DTD_NODE) {
3377 xmlDocPtr doc;
3378 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003379 if (doc != NULL) {
3380 if (doc->intSubset == (xmlDtdPtr) cur)
3381 doc->intSubset = NULL;
3382 if (doc->extSubset == (xmlDtdPtr) cur)
3383 doc->extSubset = NULL;
3384 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003385 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003386 if (cur->parent != NULL) {
3387 xmlNodePtr parent;
3388 parent = cur->parent;
3389 if (cur->type == XML_ATTRIBUTE_NODE) {
3390 if (parent->properties == (xmlAttrPtr) cur)
3391 parent->properties = ((xmlAttrPtr) cur)->next;
3392 } else {
3393 if (parent->children == cur)
3394 parent->children = cur->next;
3395 if (parent->last == cur)
3396 parent->last = cur->prev;
3397 }
3398 cur->parent = NULL;
3399 }
Owen Taylor3473f882001-02-23 17:55:21 +00003400 if (cur->next != NULL)
3401 cur->next->prev = cur->prev;
3402 if (cur->prev != NULL)
3403 cur->prev->next = cur->next;
3404 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003405}
3406
Daniel Veillard652327a2003-09-29 18:02:38 +00003407#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003408/**
3409 * xmlReplaceNode:
3410 * @old: the old node
3411 * @cur: the node
3412 *
3413 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003414 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003415 * first unlinked from its existing context.
3416 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003417 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003418 */
3419xmlNodePtr
3420xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3421 if (old == NULL) {
3422#ifdef DEBUG_TREE
3423 xmlGenericError(xmlGenericErrorContext,
3424 "xmlReplaceNode : old == NULL\n");
3425#endif
3426 return(NULL);
3427 }
3428 if (cur == NULL) {
3429 xmlUnlinkNode(old);
3430 return(old);
3431 }
3432 if (cur == old) {
3433 return(old);
3434 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003435 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3436#ifdef DEBUG_TREE
3437 xmlGenericError(xmlGenericErrorContext,
3438 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3439#endif
3440 return(old);
3441 }
3442 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3443#ifdef DEBUG_TREE
3444 xmlGenericError(xmlGenericErrorContext,
3445 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3446#endif
3447 return(old);
3448 }
Owen Taylor3473f882001-02-23 17:55:21 +00003449 xmlUnlinkNode(cur);
3450 cur->doc = old->doc;
3451 cur->parent = old->parent;
3452 cur->next = old->next;
3453 if (cur->next != NULL)
3454 cur->next->prev = cur;
3455 cur->prev = old->prev;
3456 if (cur->prev != NULL)
3457 cur->prev->next = cur;
3458 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003459 if (cur->type == XML_ATTRIBUTE_NODE) {
3460 if (cur->parent->properties == (xmlAttrPtr)old)
3461 cur->parent->properties = ((xmlAttrPtr) cur);
3462 } else {
3463 if (cur->parent->children == old)
3464 cur->parent->children = cur;
3465 if (cur->parent->last == old)
3466 cur->parent->last = cur;
3467 }
Owen Taylor3473f882001-02-23 17:55:21 +00003468 }
3469 old->next = old->prev = NULL;
3470 old->parent = NULL;
3471 return(old);
3472}
Daniel Veillard652327a2003-09-29 18:02:38 +00003473#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003474
3475/************************************************************************
3476 * *
3477 * Copy operations *
3478 * *
3479 ************************************************************************/
3480
3481/**
3482 * xmlCopyNamespace:
3483 * @cur: the namespace
3484 *
3485 * Do a copy of the namespace.
3486 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003487 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003488 */
3489xmlNsPtr
3490xmlCopyNamespace(xmlNsPtr cur) {
3491 xmlNsPtr ret;
3492
3493 if (cur == NULL) return(NULL);
3494 switch (cur->type) {
3495 case XML_LOCAL_NAMESPACE:
3496 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3497 break;
3498 default:
3499#ifdef DEBUG_TREE
3500 xmlGenericError(xmlGenericErrorContext,
3501 "xmlCopyNamespace: invalid type %d\n", cur->type);
3502#endif
3503 return(NULL);
3504 }
3505 return(ret);
3506}
3507
3508/**
3509 * xmlCopyNamespaceList:
3510 * @cur: the first namespace
3511 *
3512 * Do a copy of an namespace list.
3513 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003514 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003515 */
3516xmlNsPtr
3517xmlCopyNamespaceList(xmlNsPtr cur) {
3518 xmlNsPtr ret = NULL;
3519 xmlNsPtr p = NULL,q;
3520
3521 while (cur != NULL) {
3522 q = xmlCopyNamespace(cur);
3523 if (p == NULL) {
3524 ret = p = q;
3525 } else {
3526 p->next = q;
3527 p = q;
3528 }
3529 cur = cur->next;
3530 }
3531 return(ret);
3532}
3533
3534static xmlNodePtr
3535xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3536/**
3537 * xmlCopyProp:
3538 * @target: the element where the attribute will be grafted
3539 * @cur: the attribute
3540 *
3541 * Do a copy of the attribute.
3542 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003543 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003544 */
3545xmlAttrPtr
3546xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3547 xmlAttrPtr ret;
3548
3549 if (cur == NULL) return(NULL);
3550 if (target != NULL)
3551 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3552 else if (cur->parent != NULL)
3553 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3554 else if (cur->children != NULL)
3555 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3556 else
3557 ret = xmlNewDocProp(NULL, cur->name, NULL);
3558 if (ret == NULL) return(NULL);
3559 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003560
Owen Taylor3473f882001-02-23 17:55:21 +00003561 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003562 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003563
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003564 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3565 if (ns == NULL) {
3566 /*
3567 * Humm, we are copying an element whose namespace is defined
3568 * out of the new tree scope. Search it in the original tree
3569 * and add it at the top of the new tree
3570 */
3571 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3572 if (ns != NULL) {
3573 xmlNodePtr root = target;
3574 xmlNodePtr pred = NULL;
3575
3576 while (root->parent != NULL) {
3577 pred = root;
3578 root = root->parent;
3579 }
3580 if (root == (xmlNodePtr) target->doc) {
3581 /* correct possibly cycling above the document elt */
3582 root = pred;
3583 }
3584 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3585 }
3586 } else {
3587 /*
3588 * we have to find something appropriate here since
3589 * we cant be sure, that the namespce we found is identified
3590 * by the prefix
3591 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003592 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003593 /* this is the nice case */
3594 ret->ns = ns;
3595 } else {
3596 /*
3597 * we are in trouble: we need a new reconcilied namespace.
3598 * This is expensive
3599 */
3600 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3601 }
3602 }
3603
Owen Taylor3473f882001-02-23 17:55:21 +00003604 } else
3605 ret->ns = NULL;
3606
3607 if (cur->children != NULL) {
3608 xmlNodePtr tmp;
3609
3610 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3611 ret->last = NULL;
3612 tmp = ret->children;
3613 while (tmp != NULL) {
3614 /* tmp->parent = (xmlNodePtr)ret; */
3615 if (tmp->next == NULL)
3616 ret->last = tmp;
3617 tmp = tmp->next;
3618 }
3619 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003620 /*
3621 * Try to handle IDs
3622 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003623 if ((target!= NULL) && (cur!= NULL) &&
3624 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003625 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3626 if (xmlIsID(cur->doc, cur->parent, cur)) {
3627 xmlChar *id;
3628
3629 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3630 if (id != NULL) {
3631 xmlAddID(NULL, target->doc, id, ret);
3632 xmlFree(id);
3633 }
3634 }
3635 }
Owen Taylor3473f882001-02-23 17:55:21 +00003636 return(ret);
3637}
3638
3639/**
3640 * xmlCopyPropList:
3641 * @target: the element where the attributes will be grafted
3642 * @cur: the first attribute
3643 *
3644 * Do a copy of an attribute list.
3645 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003646 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003647 */
3648xmlAttrPtr
3649xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3650 xmlAttrPtr ret = NULL;
3651 xmlAttrPtr p = NULL,q;
3652
3653 while (cur != NULL) {
3654 q = xmlCopyProp(target, cur);
3655 if (p == NULL) {
3656 ret = p = q;
3657 } else {
3658 p->next = q;
3659 q->prev = p;
3660 p = q;
3661 }
3662 cur = cur->next;
3663 }
3664 return(ret);
3665}
3666
3667/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003668 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003669 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003670 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003671 * tricky reason: namespaces. Doing a direct copy of a node
3672 * say RPM:Copyright without changing the namespace pointer to
3673 * something else can produce stale links. One way to do it is
3674 * to keep a reference counter but this doesn't work as soon
3675 * as one move the element or the subtree out of the scope of
3676 * the existing namespace. The actual solution seems to add
3677 * a copy of the namespace at the top of the copied tree if
3678 * not available in the subtree.
3679 * Hence two functions, the public front-end call the inner ones
3680 */
3681
3682static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003683xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00003684 int recursive) {
3685 xmlNodePtr ret;
3686
3687 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003688 switch (node->type) {
3689 case XML_TEXT_NODE:
3690 case XML_CDATA_SECTION_NODE:
3691 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003692 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003693 case XML_ENTITY_REF_NODE:
3694 case XML_ENTITY_NODE:
3695 case XML_PI_NODE:
3696 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003697 case XML_XINCLUDE_START:
3698 case XML_XINCLUDE_END:
3699 break;
3700 case XML_ATTRIBUTE_NODE:
3701 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3702 case XML_NAMESPACE_DECL:
3703 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3704
Daniel Veillard39196eb2001-06-19 18:09:42 +00003705 case XML_DOCUMENT_NODE:
3706 case XML_HTML_DOCUMENT_NODE:
3707#ifdef LIBXML_DOCB_ENABLED
3708 case XML_DOCB_DOCUMENT_NODE:
3709#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003710#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003711 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard652327a2003-09-29 18:02:38 +00003712#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003713 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003714 case XML_NOTATION_NODE:
3715 case XML_DTD_NODE:
3716 case XML_ELEMENT_DECL:
3717 case XML_ATTRIBUTE_DECL:
3718 case XML_ENTITY_DECL:
3719 return(NULL);
3720 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003721
Owen Taylor3473f882001-02-23 17:55:21 +00003722 /*
3723 * Allocate a new node and fill the fields.
3724 */
3725 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3726 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003727 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003728 return(NULL);
3729 }
3730 memset(ret, 0, sizeof(xmlNode));
3731 ret->type = node->type;
3732
3733 ret->doc = doc;
3734 ret->parent = parent;
3735 if (node->name == xmlStringText)
3736 ret->name = xmlStringText;
3737 else if (node->name == xmlStringTextNoenc)
3738 ret->name = xmlStringTextNoenc;
3739 else if (node->name == xmlStringComment)
3740 ret->name = xmlStringComment;
3741 else if (node->name != NULL)
3742 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00003743 if ((node->type != XML_ELEMENT_NODE) &&
3744 (node->content != NULL) &&
3745 (node->type != XML_ENTITY_REF_NODE) &&
3746 (node->type != XML_XINCLUDE_END) &&
3747 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003748 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003749 }else{
3750 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003751 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003752 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003753 if (parent != NULL) {
3754 xmlNodePtr tmp;
3755
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003756 /*
3757 * this is a tricky part for the node register thing:
3758 * in case ret does get coalesced in xmlAddChild
3759 * the deregister-node callback is called; so we register ret now already
3760 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003761 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003762 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3763
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003764 tmp = xmlAddChild(parent, ret);
3765 /* node could have coalesced */
3766 if (tmp != ret)
3767 return(tmp);
3768 }
Owen Taylor3473f882001-02-23 17:55:21 +00003769
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003770 if (!recursive)
3771 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00003772 if (node->nsDef != NULL)
3773 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3774
3775 if (node->ns != NULL) {
3776 xmlNsPtr ns;
3777
3778 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3779 if (ns == NULL) {
3780 /*
3781 * Humm, we are copying an element whose namespace is defined
3782 * out of the new tree scope. Search it in the original tree
3783 * and add it at the top of the new tree
3784 */
3785 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3786 if (ns != NULL) {
3787 xmlNodePtr root = ret;
3788
3789 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003790 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003791 }
3792 } else {
3793 /*
3794 * reference the existing namespace definition in our own tree.
3795 */
3796 ret->ns = ns;
3797 }
3798 }
3799 if (node->properties != NULL)
3800 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003801 if (node->type == XML_ENTITY_REF_NODE) {
3802 if ((doc == NULL) || (node->doc != doc)) {
3803 /*
3804 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003805 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003806 * we cannot keep the reference. Try to find it in the
3807 * target document.
3808 */
3809 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3810 } else {
3811 ret->children = node->children;
3812 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003813 ret->last = ret->children;
3814 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003815 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003816 UPDATE_LAST_CHILD_AND_PARENT(ret)
3817 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003818
3819out:
3820 /* if parent != NULL we already registered the node above */
3821 if (parent == NULL && xmlRegisterNodeDefaultValue)
3822 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003823 return(ret);
3824}
3825
3826static xmlNodePtr
3827xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3828 xmlNodePtr ret = NULL;
3829 xmlNodePtr p = NULL,q;
3830
3831 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003832#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003833 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003834 if (doc == NULL) {
3835 node = node->next;
3836 continue;
3837 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003838 if (doc->intSubset == NULL) {
3839 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3840 q->doc = doc;
3841 q->parent = parent;
3842 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003843 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003844 } else {
3845 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003846 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003847 }
3848 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003849#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003850 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003851 if (ret == NULL) {
3852 q->prev = NULL;
3853 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003854 } else if (p != q) {
3855 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003856 p->next = q;
3857 q->prev = p;
3858 p = q;
3859 }
3860 node = node->next;
3861 }
3862 return(ret);
3863}
3864
3865/**
3866 * xmlCopyNode:
3867 * @node: the node
3868 * @recursive: if 1 do a recursive copy.
3869 *
3870 * Do a copy of the node.
3871 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003872 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003873 */
3874xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003875xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00003876 xmlNodePtr ret;
3877
3878 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
3879 return(ret);
3880}
3881
3882/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003883 * xmlDocCopyNode:
3884 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003885 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00003886 * @recursive: if 1 do a recursive copy.
3887 *
3888 * Do a copy of the node to a given document.
3889 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003890 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003891 */
3892xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003893xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003894 xmlNodePtr ret;
3895
3896 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
3897 return(ret);
3898}
3899
3900/**
Owen Taylor3473f882001-02-23 17:55:21 +00003901 * xmlCopyNodeList:
3902 * @node: the first node in the list.
3903 *
3904 * Do a recursive copy of the node list.
3905 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003906 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003907 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003908xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003909 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3910 return(ret);
3911}
3912
Daniel Veillard652327a2003-09-29 18:02:38 +00003913#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003914/**
Owen Taylor3473f882001-02-23 17:55:21 +00003915 * xmlCopyDtd:
3916 * @dtd: the dtd
3917 *
3918 * Do a copy of the dtd.
3919 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003920 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003921 */
3922xmlDtdPtr
3923xmlCopyDtd(xmlDtdPtr dtd) {
3924 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003925 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003926
3927 if (dtd == NULL) return(NULL);
3928 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3929 if (ret == NULL) return(NULL);
3930 if (dtd->entities != NULL)
3931 ret->entities = (void *) xmlCopyEntitiesTable(
3932 (xmlEntitiesTablePtr) dtd->entities);
3933 if (dtd->notations != NULL)
3934 ret->notations = (void *) xmlCopyNotationTable(
3935 (xmlNotationTablePtr) dtd->notations);
3936 if (dtd->elements != NULL)
3937 ret->elements = (void *) xmlCopyElementTable(
3938 (xmlElementTablePtr) dtd->elements);
3939 if (dtd->attributes != NULL)
3940 ret->attributes = (void *) xmlCopyAttributeTable(
3941 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003942 if (dtd->pentities != NULL)
3943 ret->pentities = (void *) xmlCopyEntitiesTable(
3944 (xmlEntitiesTablePtr) dtd->pentities);
3945
3946 cur = dtd->children;
3947 while (cur != NULL) {
3948 q = NULL;
3949
3950 if (cur->type == XML_ENTITY_DECL) {
3951 xmlEntityPtr tmp = (xmlEntityPtr) cur;
3952 switch (tmp->etype) {
3953 case XML_INTERNAL_GENERAL_ENTITY:
3954 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
3955 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
3956 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
3957 break;
3958 case XML_INTERNAL_PARAMETER_ENTITY:
3959 case XML_EXTERNAL_PARAMETER_ENTITY:
3960 q = (xmlNodePtr)
3961 xmlGetParameterEntityFromDtd(ret, tmp->name);
3962 break;
3963 case XML_INTERNAL_PREDEFINED_ENTITY:
3964 break;
3965 }
3966 } else if (cur->type == XML_ELEMENT_DECL) {
3967 xmlElementPtr tmp = (xmlElementPtr) cur;
3968 q = (xmlNodePtr)
3969 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
3970 } else if (cur->type == XML_ATTRIBUTE_DECL) {
3971 xmlAttributePtr tmp = (xmlAttributePtr) cur;
3972 q = (xmlNodePtr)
3973 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
3974 } else if (cur->type == XML_COMMENT_NODE) {
3975 q = xmlCopyNode(cur, 0);
3976 }
3977
3978 if (q == NULL) {
3979 cur = cur->next;
3980 continue;
3981 }
3982
3983 if (p == NULL)
3984 ret->children = q;
3985 else
3986 p->next = q;
3987
3988 q->prev = p;
3989 q->parent = (xmlNodePtr) ret;
3990 q->next = NULL;
3991 ret->last = q;
3992 p = q;
3993 cur = cur->next;
3994 }
3995
Owen Taylor3473f882001-02-23 17:55:21 +00003996 return(ret);
3997}
3998
3999/**
4000 * xmlCopyDoc:
4001 * @doc: the document
4002 * @recursive: if 1 do a recursive copy.
4003 *
4004 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004005 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004006 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004007 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004008 */
4009xmlDocPtr
4010xmlCopyDoc(xmlDocPtr doc, int recursive) {
4011 xmlDocPtr ret;
4012
4013 if (doc == NULL) return(NULL);
4014 ret = xmlNewDoc(doc->version);
4015 if (ret == NULL) return(NULL);
4016 if (doc->name != NULL)
4017 ret->name = xmlMemStrdup(doc->name);
4018 if (doc->encoding != NULL)
4019 ret->encoding = xmlStrdup(doc->encoding);
4020 ret->charset = doc->charset;
4021 ret->compression = doc->compression;
4022 ret->standalone = doc->standalone;
4023 if (!recursive) return(ret);
4024
Daniel Veillardb33c2012001-04-25 12:59:04 +00004025 ret->last = NULL;
4026 ret->children = NULL;
4027 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004028 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004029 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004030 ret->intSubset->parent = ret;
4031 }
Owen Taylor3473f882001-02-23 17:55:21 +00004032 if (doc->oldNs != NULL)
4033 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4034 if (doc->children != NULL) {
4035 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004036
4037 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4038 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004039 ret->last = NULL;
4040 tmp = ret->children;
4041 while (tmp != NULL) {
4042 if (tmp->next == NULL)
4043 ret->last = tmp;
4044 tmp = tmp->next;
4045 }
4046 }
4047 return(ret);
4048}
Daniel Veillard652327a2003-09-29 18:02:38 +00004049#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004050
4051/************************************************************************
4052 * *
4053 * Content access functions *
4054 * *
4055 ************************************************************************/
4056
4057/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004058 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004059 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004060 *
4061 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004062 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004063 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004064 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004065 */
4066long
4067xmlGetLineNo(xmlNodePtr node)
4068{
4069 long result = -1;
4070
4071 if (!node)
4072 return result;
4073 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004074 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004075 else if ((node->prev != NULL) &&
4076 ((node->prev->type == XML_ELEMENT_NODE) ||
4077 (node->prev->type == XML_TEXT_NODE)))
4078 result = xmlGetLineNo(node->prev);
4079 else if ((node->parent != NULL) &&
4080 ((node->parent->type == XML_ELEMENT_NODE) ||
4081 (node->parent->type == XML_TEXT_NODE)))
4082 result = xmlGetLineNo(node->parent);
4083
4084 return result;
4085}
4086
Daniel Veillard652327a2003-09-29 18:02:38 +00004087#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8faa7832001-11-26 15:58:08 +00004088/**
4089 * xmlGetNodePath:
4090 * @node: a node
4091 *
4092 * Build a structure based Path for the given node
4093 *
4094 * Returns the new path or NULL in case of error. The caller must free
4095 * the returned string
4096 */
4097xmlChar *
4098xmlGetNodePath(xmlNodePtr node)
4099{
4100 xmlNodePtr cur, tmp, next;
4101 xmlChar *buffer = NULL, *temp;
4102 size_t buf_len;
4103 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004104 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004105 const char *name;
4106 char nametemp[100];
4107 int occur = 0;
4108
4109 if (node == NULL)
4110 return (NULL);
4111
4112 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004113 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004114 if (buffer == NULL) {
4115 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004116 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004117 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004118 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004119 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004120 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004121 xmlFree(buffer);
4122 return (NULL);
4123 }
4124
4125 buffer[0] = 0;
4126 cur = node;
4127 do {
4128 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004129 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004130 occur = 0;
4131 if ((cur->type == XML_DOCUMENT_NODE) ||
4132 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4133 if (buffer[0] == '/')
4134 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004135 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004136 next = NULL;
4137 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004138 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004139 name = (const char *) cur->name;
4140 if (cur->ns) {
4141 snprintf(nametemp, sizeof(nametemp) - 1,
4142 "%s:%s", cur->ns->prefix, cur->name);
4143 nametemp[sizeof(nametemp) - 1] = 0;
4144 name = nametemp;
4145 }
4146 next = cur->parent;
4147
4148 /*
4149 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004150 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004151 */
4152 tmp = cur->prev;
4153 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004154 if ((tmp->type == XML_ELEMENT_NODE) &&
4155 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004156 occur++;
4157 tmp = tmp->prev;
4158 }
4159 if (occur == 0) {
4160 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004161 while (tmp != NULL && occur == 0) {
4162 if ((tmp->type == XML_ELEMENT_NODE) &&
4163 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004164 occur++;
4165 tmp = tmp->next;
4166 }
4167 if (occur != 0)
4168 occur = 1;
4169 } else
4170 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004171 } else if (cur->type == XML_COMMENT_NODE) {
4172 sep = "/";
4173 name = "comment()";
4174 next = cur->parent;
4175
4176 /*
4177 * Thumbler index computation
4178 */
4179 tmp = cur->prev;
4180 while (tmp != NULL) {
4181 if (tmp->type == XML_COMMENT_NODE)
4182 occur++;
4183 tmp = tmp->prev;
4184 }
4185 if (occur == 0) {
4186 tmp = cur->next;
4187 while (tmp != NULL && occur == 0) {
4188 if (tmp->type == XML_COMMENT_NODE)
4189 occur++;
4190 tmp = tmp->next;
4191 }
4192 if (occur != 0)
4193 occur = 1;
4194 } else
4195 occur++;
4196 } else if ((cur->type == XML_TEXT_NODE) ||
4197 (cur->type == XML_CDATA_SECTION_NODE)) {
4198 sep = "/";
4199 name = "text()";
4200 next = cur->parent;
4201
4202 /*
4203 * Thumbler index computation
4204 */
4205 tmp = cur->prev;
4206 while (tmp != NULL) {
4207 if ((cur->type == XML_TEXT_NODE) ||
4208 (cur->type == XML_CDATA_SECTION_NODE))
4209 occur++;
4210 tmp = tmp->prev;
4211 }
4212 if (occur == 0) {
4213 tmp = cur->next;
4214 while (tmp != NULL && occur == 0) {
4215 if ((cur->type == XML_TEXT_NODE) ||
4216 (cur->type == XML_CDATA_SECTION_NODE))
4217 occur++;
4218 tmp = tmp->next;
4219 }
4220 if (occur != 0)
4221 occur = 1;
4222 } else
4223 occur++;
4224 } else if (cur->type == XML_PI_NODE) {
4225 sep = "/";
4226 snprintf(nametemp, sizeof(nametemp) - 1,
4227 "processing-instruction('%s')", cur->name);
4228 nametemp[sizeof(nametemp) - 1] = 0;
4229 name = nametemp;
4230
4231 next = cur->parent;
4232
4233 /*
4234 * Thumbler index computation
4235 */
4236 tmp = cur->prev;
4237 while (tmp != NULL) {
4238 if ((tmp->type == XML_PI_NODE) &&
4239 (xmlStrEqual(cur->name, tmp->name)))
4240 occur++;
4241 tmp = tmp->prev;
4242 }
4243 if (occur == 0) {
4244 tmp = cur->next;
4245 while (tmp != NULL && occur == 0) {
4246 if ((tmp->type == XML_PI_NODE) &&
4247 (xmlStrEqual(cur->name, tmp->name)))
4248 occur++;
4249 tmp = tmp->next;
4250 }
4251 if (occur != 0)
4252 occur = 1;
4253 } else
4254 occur++;
4255
Daniel Veillard8faa7832001-11-26 15:58:08 +00004256 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004257 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004258 name = (const char *) (((xmlAttrPtr) cur)->name);
4259 next = ((xmlAttrPtr) cur)->parent;
4260 } else {
4261 next = cur->parent;
4262 }
4263
4264 /*
4265 * Make sure there is enough room
4266 */
4267 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4268 buf_len =
4269 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4270 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4271 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004272 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004273 xmlFree(buf);
4274 xmlFree(buffer);
4275 return (NULL);
4276 }
4277 buffer = temp;
4278 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4279 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004280 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004281 xmlFree(buf);
4282 xmlFree(buffer);
4283 return (NULL);
4284 }
4285 buf = temp;
4286 }
4287 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004288 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004289 sep, name, (char *) buffer);
4290 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004291 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004292 sep, name, occur, (char *) buffer);
4293 snprintf((char *) buffer, buf_len, "%s", buf);
4294 cur = next;
4295 } while (cur != NULL);
4296 xmlFree(buf);
4297 return (buffer);
4298}
Daniel Veillard652327a2003-09-29 18:02:38 +00004299#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004300
4301/**
Owen Taylor3473f882001-02-23 17:55:21 +00004302 * xmlDocGetRootElement:
4303 * @doc: the document
4304 *
4305 * Get the root element of the document (doc->children is a list
4306 * containing possibly comments, PIs, etc ...).
4307 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004308 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004309 */
4310xmlNodePtr
4311xmlDocGetRootElement(xmlDocPtr doc) {
4312 xmlNodePtr ret;
4313
4314 if (doc == NULL) return(NULL);
4315 ret = doc->children;
4316 while (ret != NULL) {
4317 if (ret->type == XML_ELEMENT_NODE)
4318 return(ret);
4319 ret = ret->next;
4320 }
4321 return(ret);
4322}
4323
Daniel Veillard652327a2003-09-29 18:02:38 +00004324#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004325/**
4326 * xmlDocSetRootElement:
4327 * @doc: the document
4328 * @root: the new document root element
4329 *
4330 * Set the root element of the document (doc->children is a list
4331 * containing possibly comments, PIs, etc ...).
4332 *
4333 * Returns the old root element if any was found
4334 */
4335xmlNodePtr
4336xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4337 xmlNodePtr old = NULL;
4338
4339 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004340 if (root == NULL)
4341 return(NULL);
4342 xmlUnlinkNode(root);
4343 root->doc = doc;
4344 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004345 old = doc->children;
4346 while (old != NULL) {
4347 if (old->type == XML_ELEMENT_NODE)
4348 break;
4349 old = old->next;
4350 }
4351 if (old == NULL) {
4352 if (doc->children == NULL) {
4353 doc->children = root;
4354 doc->last = root;
4355 } else {
4356 xmlAddSibling(doc->children, root);
4357 }
4358 } else {
4359 xmlReplaceNode(old, root);
4360 }
4361 return(old);
4362}
4363
4364/**
4365 * xmlNodeSetLang:
4366 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004367 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004368 *
4369 * Set the language of a node, i.e. the values of the xml:lang
4370 * attribute.
4371 */
4372void
4373xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004374 xmlNsPtr ns;
4375
Owen Taylor3473f882001-02-23 17:55:21 +00004376 if (cur == NULL) return;
4377 switch(cur->type) {
4378 case XML_TEXT_NODE:
4379 case XML_CDATA_SECTION_NODE:
4380 case XML_COMMENT_NODE:
4381 case XML_DOCUMENT_NODE:
4382 case XML_DOCUMENT_TYPE_NODE:
4383 case XML_DOCUMENT_FRAG_NODE:
4384 case XML_NOTATION_NODE:
4385 case XML_HTML_DOCUMENT_NODE:
4386 case XML_DTD_NODE:
4387 case XML_ELEMENT_DECL:
4388 case XML_ATTRIBUTE_DECL:
4389 case XML_ENTITY_DECL:
4390 case XML_PI_NODE:
4391 case XML_ENTITY_REF_NODE:
4392 case XML_ENTITY_NODE:
4393 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004394#ifdef LIBXML_DOCB_ENABLED
4395 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004396#endif
4397 case XML_XINCLUDE_START:
4398 case XML_XINCLUDE_END:
4399 return;
4400 case XML_ELEMENT_NODE:
4401 case XML_ATTRIBUTE_NODE:
4402 break;
4403 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004404 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4405 if (ns == NULL)
4406 return;
4407 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004408}
Daniel Veillard652327a2003-09-29 18:02:38 +00004409#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004410
4411/**
4412 * xmlNodeGetLang:
4413 * @cur: the node being checked
4414 *
4415 * Searches the language of a node, i.e. the values of the xml:lang
4416 * attribute or the one carried by the nearest ancestor.
4417 *
4418 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004419 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004420 */
4421xmlChar *
4422xmlNodeGetLang(xmlNodePtr cur) {
4423 xmlChar *lang;
4424
4425 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004426 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004427 if (lang != NULL)
4428 return(lang);
4429 cur = cur->parent;
4430 }
4431 return(NULL);
4432}
4433
4434
Daniel Veillard652327a2003-09-29 18:02:38 +00004435#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004436/**
4437 * xmlNodeSetSpacePreserve:
4438 * @cur: the node being changed
4439 * @val: the xml:space value ("0": default, 1: "preserve")
4440 *
4441 * Set (or reset) the space preserving behaviour of a node, i.e. the
4442 * value of the xml:space attribute.
4443 */
4444void
4445xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004446 xmlNsPtr ns;
4447
Owen Taylor3473f882001-02-23 17:55:21 +00004448 if (cur == NULL) return;
4449 switch(cur->type) {
4450 case XML_TEXT_NODE:
4451 case XML_CDATA_SECTION_NODE:
4452 case XML_COMMENT_NODE:
4453 case XML_DOCUMENT_NODE:
4454 case XML_DOCUMENT_TYPE_NODE:
4455 case XML_DOCUMENT_FRAG_NODE:
4456 case XML_NOTATION_NODE:
4457 case XML_HTML_DOCUMENT_NODE:
4458 case XML_DTD_NODE:
4459 case XML_ELEMENT_DECL:
4460 case XML_ATTRIBUTE_DECL:
4461 case XML_ENTITY_DECL:
4462 case XML_PI_NODE:
4463 case XML_ENTITY_REF_NODE:
4464 case XML_ENTITY_NODE:
4465 case XML_NAMESPACE_DECL:
4466 case XML_XINCLUDE_START:
4467 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004468#ifdef LIBXML_DOCB_ENABLED
4469 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004470#endif
4471 return;
4472 case XML_ELEMENT_NODE:
4473 case XML_ATTRIBUTE_NODE:
4474 break;
4475 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004476 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4477 if (ns == NULL)
4478 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004479 switch (val) {
4480 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004481 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004482 break;
4483 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004484 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004485 break;
4486 }
4487}
Daniel Veillard652327a2003-09-29 18:02:38 +00004488#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004489
4490/**
4491 * xmlNodeGetSpacePreserve:
4492 * @cur: the node being checked
4493 *
4494 * Searches the space preserving behaviour of a node, i.e. the values
4495 * of the xml:space attribute or the one carried by the nearest
4496 * ancestor.
4497 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004498 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004499 */
4500int
4501xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4502 xmlChar *space;
4503
4504 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004505 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004506 if (space != NULL) {
4507 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4508 xmlFree(space);
4509 return(1);
4510 }
4511 if (xmlStrEqual(space, BAD_CAST "default")) {
4512 xmlFree(space);
4513 return(0);
4514 }
4515 xmlFree(space);
4516 }
4517 cur = cur->parent;
4518 }
4519 return(-1);
4520}
4521
Daniel Veillard652327a2003-09-29 18:02:38 +00004522#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004523/**
4524 * xmlNodeSetName:
4525 * @cur: the node being changed
4526 * @name: the new tag name
4527 *
4528 * Set (or reset) the name of a node.
4529 */
4530void
4531xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
4532 if (cur == NULL) return;
4533 if (name == NULL) return;
4534 switch(cur->type) {
4535 case XML_TEXT_NODE:
4536 case XML_CDATA_SECTION_NODE:
4537 case XML_COMMENT_NODE:
4538 case XML_DOCUMENT_TYPE_NODE:
4539 case XML_DOCUMENT_FRAG_NODE:
4540 case XML_NOTATION_NODE:
4541 case XML_HTML_DOCUMENT_NODE:
4542 case XML_NAMESPACE_DECL:
4543 case XML_XINCLUDE_START:
4544 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004545#ifdef LIBXML_DOCB_ENABLED
4546 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004547#endif
4548 return;
4549 case XML_ELEMENT_NODE:
4550 case XML_ATTRIBUTE_NODE:
4551 case XML_PI_NODE:
4552 case XML_ENTITY_REF_NODE:
4553 case XML_ENTITY_NODE:
4554 case XML_DTD_NODE:
4555 case XML_DOCUMENT_NODE:
4556 case XML_ELEMENT_DECL:
4557 case XML_ATTRIBUTE_DECL:
4558 case XML_ENTITY_DECL:
4559 break;
4560 }
4561 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4562 cur->name = xmlStrdup(name);
4563}
4564
4565/**
4566 * xmlNodeSetBase:
4567 * @cur: the node being changed
4568 * @uri: the new base URI
4569 *
4570 * Set (or reset) the base URI of a node, i.e. the value of the
4571 * xml:base attribute.
4572 */
4573void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004574xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004575 xmlNsPtr ns;
4576
Owen Taylor3473f882001-02-23 17:55:21 +00004577 if (cur == NULL) return;
4578 switch(cur->type) {
4579 case XML_TEXT_NODE:
4580 case XML_CDATA_SECTION_NODE:
4581 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004582 case XML_DOCUMENT_TYPE_NODE:
4583 case XML_DOCUMENT_FRAG_NODE:
4584 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004585 case XML_DTD_NODE:
4586 case XML_ELEMENT_DECL:
4587 case XML_ATTRIBUTE_DECL:
4588 case XML_ENTITY_DECL:
4589 case XML_PI_NODE:
4590 case XML_ENTITY_REF_NODE:
4591 case XML_ENTITY_NODE:
4592 case XML_NAMESPACE_DECL:
4593 case XML_XINCLUDE_START:
4594 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004595 return;
4596 case XML_ELEMENT_NODE:
4597 case XML_ATTRIBUTE_NODE:
4598 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004599 case XML_DOCUMENT_NODE:
4600#ifdef LIBXML_DOCB_ENABLED
4601 case XML_DOCB_DOCUMENT_NODE:
4602#endif
4603 case XML_HTML_DOCUMENT_NODE: {
4604 xmlDocPtr doc = (xmlDocPtr) cur;
4605
4606 if (doc->URL != NULL)
4607 xmlFree((xmlChar *) doc->URL);
4608 if (uri == NULL)
4609 doc->URL = NULL;
4610 else
4611 doc->URL = xmlStrdup(uri);
4612 return;
4613 }
Owen Taylor3473f882001-02-23 17:55:21 +00004614 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004615
4616 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4617 if (ns == NULL)
4618 return;
4619 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004620}
Daniel Veillard652327a2003-09-29 18:02:38 +00004621#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004622
4623/**
Owen Taylor3473f882001-02-23 17:55:21 +00004624 * xmlNodeGetBase:
4625 * @doc: the document the node pertains to
4626 * @cur: the node being checked
4627 *
4628 * Searches for the BASE URL. The code should work on both XML
4629 * and HTML document even if base mechanisms are completely different.
4630 * It returns the base as defined in RFC 2396 sections
4631 * 5.1.1. Base URI within Document Content
4632 * and
4633 * 5.1.2. Base URI from the Encapsulating Entity
4634 * However it does not return the document base (5.1.3), use
4635 * xmlDocumentGetBase() for this
4636 *
4637 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004638 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004639 */
4640xmlChar *
4641xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004642 xmlChar *oldbase = NULL;
4643 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004644
4645 if ((cur == NULL) && (doc == NULL))
4646 return(NULL);
4647 if (doc == NULL) doc = cur->doc;
4648 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4649 cur = doc->children;
4650 while ((cur != NULL) && (cur->name != NULL)) {
4651 if (cur->type != XML_ELEMENT_NODE) {
4652 cur = cur->next;
4653 continue;
4654 }
4655 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4656 cur = cur->children;
4657 continue;
4658 }
4659 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4660 cur = cur->children;
4661 continue;
4662 }
4663 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4664 return(xmlGetProp(cur, BAD_CAST "href"));
4665 }
4666 cur = cur->next;
4667 }
4668 return(NULL);
4669 }
4670 while (cur != NULL) {
4671 if (cur->type == XML_ENTITY_DECL) {
4672 xmlEntityPtr ent = (xmlEntityPtr) cur;
4673 return(xmlStrdup(ent->URI));
4674 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004675 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004676 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004677 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004678 if (oldbase != NULL) {
4679 newbase = xmlBuildURI(oldbase, base);
4680 if (newbase != NULL) {
4681 xmlFree(oldbase);
4682 xmlFree(base);
4683 oldbase = newbase;
4684 } else {
4685 xmlFree(oldbase);
4686 xmlFree(base);
4687 return(NULL);
4688 }
4689 } else {
4690 oldbase = base;
4691 }
4692 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4693 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4694 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4695 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004696 }
4697 }
Owen Taylor3473f882001-02-23 17:55:21 +00004698 cur = cur->parent;
4699 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004700 if ((doc != NULL) && (doc->URL != NULL)) {
4701 if (oldbase == NULL)
4702 return(xmlStrdup(doc->URL));
4703 newbase = xmlBuildURI(oldbase, doc->URL);
4704 xmlFree(oldbase);
4705 return(newbase);
4706 }
4707 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004708}
4709
4710/**
Daniel Veillard78697292003-10-19 20:44:43 +00004711 * xmlNodeBufGetContent:
4712 * @buffer: a buffer
4713 * @cur: the node being read
4714 *
4715 * Read the value of a node @cur, this can be either the text carried
4716 * directly by this node if it's a TEXT node or the aggregate string
4717 * of the values carried by this node child's (TEXT and ENTITY_REF).
4718 * Entity references are substituted.
4719 * Fills up the buffer @buffer with this value
4720 *
4721 * Returns 0 in case of success and -1 in case of error.
4722 */
4723int
4724xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4725{
4726 if ((cur == NULL) || (buffer == NULL)) return(-1);
4727 switch (cur->type) {
4728 case XML_CDATA_SECTION_NODE:
4729 case XML_TEXT_NODE:
4730 xmlBufferCat(buffer, cur->content);
4731 break;
4732 case XML_DOCUMENT_FRAG_NODE:
4733 case XML_ELEMENT_NODE:{
4734 xmlNodePtr tmp = cur;
4735
4736 while (tmp != NULL) {
4737 switch (tmp->type) {
4738 case XML_CDATA_SECTION_NODE:
4739 case XML_TEXT_NODE:
4740 if (tmp->content != NULL)
4741 xmlBufferCat(buffer, tmp->content);
4742 break;
4743 case XML_ENTITY_REF_NODE:
4744 xmlNodeBufGetContent(buffer, tmp->children);
4745 break;
4746 default:
4747 break;
4748 }
4749 /*
4750 * Skip to next node
4751 */
4752 if (tmp->children != NULL) {
4753 if (tmp->children->type != XML_ENTITY_DECL) {
4754 tmp = tmp->children;
4755 continue;
4756 }
4757 }
4758 if (tmp == cur)
4759 break;
4760
4761 if (tmp->next != NULL) {
4762 tmp = tmp->next;
4763 continue;
4764 }
4765
4766 do {
4767 tmp = tmp->parent;
4768 if (tmp == NULL)
4769 break;
4770 if (tmp == cur) {
4771 tmp = NULL;
4772 break;
4773 }
4774 if (tmp->next != NULL) {
4775 tmp = tmp->next;
4776 break;
4777 }
4778 } while (tmp != NULL);
4779 }
4780 break;
4781 }
4782 case XML_ATTRIBUTE_NODE:{
4783 xmlAttrPtr attr = (xmlAttrPtr) cur;
4784 xmlNodePtr tmp = attr->children;
4785
4786 while (tmp != NULL) {
4787 if (tmp->type == XML_TEXT_NODE)
4788 xmlBufferCat(buffer, tmp->content);
4789 else
4790 xmlNodeBufGetContent(buffer, tmp);
4791 tmp = tmp->next;
4792 }
4793 break;
4794 }
4795 case XML_COMMENT_NODE:
4796 case XML_PI_NODE:
4797 xmlBufferCat(buffer, cur->content);
4798 break;
4799 case XML_ENTITY_REF_NODE:{
4800 xmlEntityPtr ent;
4801 xmlNodePtr tmp;
4802
4803 /* lookup entity declaration */
4804 ent = xmlGetDocEntity(cur->doc, cur->name);
4805 if (ent == NULL)
4806 return(-1);
4807
4808 /* an entity content can be any "well balanced chunk",
4809 * i.e. the result of the content [43] production:
4810 * http://www.w3.org/TR/REC-xml#NT-content
4811 * -> we iterate through child nodes and recursive call
4812 * xmlNodeGetContent() which handles all possible node types */
4813 tmp = ent->children;
4814 while (tmp) {
4815 xmlNodeBufGetContent(buffer, tmp);
4816 tmp = tmp->next;
4817 }
4818 break;
4819 }
4820 case XML_ENTITY_NODE:
4821 case XML_DOCUMENT_TYPE_NODE:
4822 case XML_NOTATION_NODE:
4823 case XML_DTD_NODE:
4824 case XML_XINCLUDE_START:
4825 case XML_XINCLUDE_END:
4826 break;
4827 case XML_DOCUMENT_NODE:
4828#ifdef LIBXML_DOCB_ENABLED
4829 case XML_DOCB_DOCUMENT_NODE:
4830#endif
4831 case XML_HTML_DOCUMENT_NODE:
4832 cur = cur->children;
4833 while (cur!= NULL) {
4834 if ((cur->type == XML_ELEMENT_NODE) ||
4835 (cur->type == XML_TEXT_NODE) ||
4836 (cur->type == XML_CDATA_SECTION_NODE)) {
4837 xmlNodeBufGetContent(buffer, cur);
4838 }
4839 cur = cur->next;
4840 }
4841 break;
4842 case XML_NAMESPACE_DECL:
4843 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
4844 break;
4845 case XML_ELEMENT_DECL:
4846 case XML_ATTRIBUTE_DECL:
4847 case XML_ENTITY_DECL:
4848 break;
4849 }
4850 return(0);
4851}
4852/**
Owen Taylor3473f882001-02-23 17:55:21 +00004853 * xmlNodeGetContent:
4854 * @cur: the node being read
4855 *
4856 * Read the value of a node, this can be either the text carried
4857 * directly by this node if it's a TEXT node or the aggregate string
4858 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004859 * Entity references are substituted.
4860 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004861 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004862 */
4863xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004864xmlNodeGetContent(xmlNodePtr cur)
4865{
4866 if (cur == NULL)
4867 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004868 switch (cur->type) {
4869 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004870 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00004871 xmlBufferPtr buffer;
4872 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004873
Daniel Veillard814a76d2003-01-23 18:24:20 +00004874 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004875 if (buffer == NULL)
4876 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00004877 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004878 ret = buffer->content;
4879 buffer->content = NULL;
4880 xmlBufferFree(buffer);
4881 return (ret);
4882 }
4883 case XML_ATTRIBUTE_NODE:{
4884 xmlAttrPtr attr = (xmlAttrPtr) cur;
4885
4886 if (attr->parent != NULL)
4887 return (xmlNodeListGetString
4888 (attr->parent->doc, attr->children, 1));
4889 else
4890 return (xmlNodeListGetString(NULL, attr->children, 1));
4891 break;
4892 }
Owen Taylor3473f882001-02-23 17:55:21 +00004893 case XML_COMMENT_NODE:
4894 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004895 if (cur->content != NULL)
4896 return (xmlStrdup(cur->content));
4897 return (NULL);
4898 case XML_ENTITY_REF_NODE:{
4899 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00004900 xmlBufferPtr buffer;
4901 xmlChar *ret;
4902
4903 /* lookup entity declaration */
4904 ent = xmlGetDocEntity(cur->doc, cur->name);
4905 if (ent == NULL)
4906 return (NULL);
4907
4908 buffer = xmlBufferCreate();
4909 if (buffer == NULL)
4910 return (NULL);
4911
Daniel Veillardc4696922003-10-19 21:47:14 +00004912 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004913
4914 ret = buffer->content;
4915 buffer->content = NULL;
4916 xmlBufferFree(buffer);
4917 return (ret);
4918 }
Owen Taylor3473f882001-02-23 17:55:21 +00004919 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004920 case XML_DOCUMENT_TYPE_NODE:
4921 case XML_NOTATION_NODE:
4922 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004923 case XML_XINCLUDE_START:
4924 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00004925 return (NULL);
4926 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004927#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00004928 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004929#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00004930 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00004931 xmlBufferPtr buffer;
4932 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00004933
Daniel Veillardc4696922003-10-19 21:47:14 +00004934 buffer = xmlBufferCreate();
4935 if (buffer == NULL)
4936 return (NULL);
4937
4938 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
4939
4940 ret = buffer->content;
4941 buffer->content = NULL;
4942 xmlBufferFree(buffer);
4943 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00004944 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004945 case XML_NAMESPACE_DECL: {
4946 xmlChar *tmp;
4947
4948 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
4949 return (tmp);
4950 }
Owen Taylor3473f882001-02-23 17:55:21 +00004951 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004952 /* TODO !!! */
4953 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004954 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004955 /* TODO !!! */
4956 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004957 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004958 /* TODO !!! */
4959 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004960 case XML_CDATA_SECTION_NODE:
4961 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004962 if (cur->content != NULL)
4963 return (xmlStrdup(cur->content));
4964 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004965 }
Daniel Veillard7646b182002-04-20 06:41:40 +00004966 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004967}
Daniel Veillard652327a2003-09-29 18:02:38 +00004968
Owen Taylor3473f882001-02-23 17:55:21 +00004969/**
4970 * xmlNodeSetContent:
4971 * @cur: the node being modified
4972 * @content: the new value of the content
4973 *
4974 * Replace the content of a node.
4975 */
4976void
4977xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
4978 if (cur == NULL) {
4979#ifdef DEBUG_TREE
4980 xmlGenericError(xmlGenericErrorContext,
4981 "xmlNodeSetContent : node == NULL\n");
4982#endif
4983 return;
4984 }
4985 switch (cur->type) {
4986 case XML_DOCUMENT_FRAG_NODE:
4987 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004988 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004989 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4990 cur->children = xmlStringGetNodeList(cur->doc, content);
4991 UPDATE_LAST_CHILD_AND_PARENT(cur)
4992 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004993 case XML_TEXT_NODE:
4994 case XML_CDATA_SECTION_NODE:
4995 case XML_ENTITY_REF_NODE:
4996 case XML_ENTITY_NODE:
4997 case XML_PI_NODE:
4998 case XML_COMMENT_NODE:
4999 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005000 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005001 }
5002 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5003 cur->last = cur->children = NULL;
5004 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005005 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005006 } else
5007 cur->content = NULL;
5008 break;
5009 case XML_DOCUMENT_NODE:
5010 case XML_HTML_DOCUMENT_NODE:
5011 case XML_DOCUMENT_TYPE_NODE:
5012 case XML_XINCLUDE_START:
5013 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005014#ifdef LIBXML_DOCB_ENABLED
5015 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005016#endif
5017 break;
5018 case XML_NOTATION_NODE:
5019 break;
5020 case XML_DTD_NODE:
5021 break;
5022 case XML_NAMESPACE_DECL:
5023 break;
5024 case XML_ELEMENT_DECL:
5025 /* TODO !!! */
5026 break;
5027 case XML_ATTRIBUTE_DECL:
5028 /* TODO !!! */
5029 break;
5030 case XML_ENTITY_DECL:
5031 /* TODO !!! */
5032 break;
5033 }
5034}
5035
Daniel Veillard652327a2003-09-29 18:02:38 +00005036#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005037/**
5038 * xmlNodeSetContentLen:
5039 * @cur: the node being modified
5040 * @content: the new value of the content
5041 * @len: the size of @content
5042 *
5043 * Replace the content of a node.
5044 */
5045void
5046xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5047 if (cur == NULL) {
5048#ifdef DEBUG_TREE
5049 xmlGenericError(xmlGenericErrorContext,
5050 "xmlNodeSetContentLen : node == NULL\n");
5051#endif
5052 return;
5053 }
5054 switch (cur->type) {
5055 case XML_DOCUMENT_FRAG_NODE:
5056 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005057 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005058 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5059 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5060 UPDATE_LAST_CHILD_AND_PARENT(cur)
5061 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005062 case XML_TEXT_NODE:
5063 case XML_CDATA_SECTION_NODE:
5064 case XML_ENTITY_REF_NODE:
5065 case XML_ENTITY_NODE:
5066 case XML_PI_NODE:
5067 case XML_COMMENT_NODE:
5068 case XML_NOTATION_NODE:
5069 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005070 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005071 }
5072 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5073 cur->children = cur->last = NULL;
5074 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005075 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005076 } else
5077 cur->content = NULL;
5078 break;
5079 case XML_DOCUMENT_NODE:
5080 case XML_DTD_NODE:
5081 case XML_HTML_DOCUMENT_NODE:
5082 case XML_DOCUMENT_TYPE_NODE:
5083 case XML_NAMESPACE_DECL:
5084 case XML_XINCLUDE_START:
5085 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005086#ifdef LIBXML_DOCB_ENABLED
5087 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005088#endif
5089 break;
5090 case XML_ELEMENT_DECL:
5091 /* TODO !!! */
5092 break;
5093 case XML_ATTRIBUTE_DECL:
5094 /* TODO !!! */
5095 break;
5096 case XML_ENTITY_DECL:
5097 /* TODO !!! */
5098 break;
5099 }
5100}
Daniel Veillard652327a2003-09-29 18:02:38 +00005101#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005102
5103/**
5104 * xmlNodeAddContentLen:
5105 * @cur: the node being modified
5106 * @content: extra content
5107 * @len: the size of @content
5108 *
5109 * Append the extra substring to the node content.
5110 */
5111void
5112xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5113 if (cur == NULL) {
5114#ifdef DEBUG_TREE
5115 xmlGenericError(xmlGenericErrorContext,
5116 "xmlNodeAddContentLen : node == NULL\n");
5117#endif
5118 return;
5119 }
5120 if (len <= 0) return;
5121 switch (cur->type) {
5122 case XML_DOCUMENT_FRAG_NODE:
5123 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005124 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005125
Daniel Veillard7db37732001-07-12 01:20:08 +00005126 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005127 newNode = xmlNewTextLen(content, len);
5128 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005129 tmp = xmlAddChild(cur, newNode);
5130 if (tmp != newNode)
5131 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005132 if ((last != NULL) && (last->next == newNode)) {
5133 xmlTextMerge(last, newNode);
5134 }
5135 }
5136 break;
5137 }
5138 case XML_ATTRIBUTE_NODE:
5139 break;
5140 case XML_TEXT_NODE:
5141 case XML_CDATA_SECTION_NODE:
5142 case XML_ENTITY_REF_NODE:
5143 case XML_ENTITY_NODE:
5144 case XML_PI_NODE:
5145 case XML_COMMENT_NODE:
5146 case XML_NOTATION_NODE:
5147 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005148 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005149 }
5150 case XML_DOCUMENT_NODE:
5151 case XML_DTD_NODE:
5152 case XML_HTML_DOCUMENT_NODE:
5153 case XML_DOCUMENT_TYPE_NODE:
5154 case XML_NAMESPACE_DECL:
5155 case XML_XINCLUDE_START:
5156 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005157#ifdef LIBXML_DOCB_ENABLED
5158 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005159#endif
5160 break;
5161 case XML_ELEMENT_DECL:
5162 case XML_ATTRIBUTE_DECL:
5163 case XML_ENTITY_DECL:
5164 break;
5165 }
5166}
5167
5168/**
5169 * xmlNodeAddContent:
5170 * @cur: the node being modified
5171 * @content: extra content
5172 *
5173 * Append the extra substring to the node content.
5174 */
5175void
5176xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5177 int len;
5178
5179 if (cur == NULL) {
5180#ifdef DEBUG_TREE
5181 xmlGenericError(xmlGenericErrorContext,
5182 "xmlNodeAddContent : node == NULL\n");
5183#endif
5184 return;
5185 }
5186 if (content == NULL) return;
5187 len = xmlStrlen(content);
5188 xmlNodeAddContentLen(cur, content, len);
5189}
5190
5191/**
5192 * xmlTextMerge:
5193 * @first: the first text node
5194 * @second: the second text node being merged
5195 *
5196 * Merge two text nodes into one
5197 * Returns the first text node augmented
5198 */
5199xmlNodePtr
5200xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5201 if (first == NULL) return(second);
5202 if (second == NULL) return(first);
5203 if (first->type != XML_TEXT_NODE) return(first);
5204 if (second->type != XML_TEXT_NODE) return(first);
5205 if (second->name != first->name)
5206 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005207 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005208 xmlUnlinkNode(second);
5209 xmlFreeNode(second);
5210 return(first);
5211}
5212
Daniel Veillard652327a2003-09-29 18:02:38 +00005213#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005214/**
5215 * xmlGetNsList:
5216 * @doc: the document
5217 * @node: the current node
5218 *
5219 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005220 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005221 * that need to be freed by the caller or NULL if no
5222 * namespace if defined
5223 */
5224xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005225xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5226{
Owen Taylor3473f882001-02-23 17:55:21 +00005227 xmlNsPtr cur;
5228 xmlNsPtr *ret = NULL;
5229 int nbns = 0;
5230 int maxns = 10;
5231 int i;
5232
5233 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005234 if (node->type == XML_ELEMENT_NODE) {
5235 cur = node->nsDef;
5236 while (cur != NULL) {
5237 if (ret == NULL) {
5238 ret =
5239 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5240 sizeof(xmlNsPtr));
5241 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005242 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005243 return (NULL);
5244 }
5245 ret[nbns] = NULL;
5246 }
5247 for (i = 0; i < nbns; i++) {
5248 if ((cur->prefix == ret[i]->prefix) ||
5249 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5250 break;
5251 }
5252 if (i >= nbns) {
5253 if (nbns >= maxns) {
5254 maxns *= 2;
5255 ret = (xmlNsPtr *) xmlRealloc(ret,
5256 (maxns +
5257 1) *
5258 sizeof(xmlNsPtr));
5259 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005260 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005261 return (NULL);
5262 }
5263 }
5264 ret[nbns++] = cur;
5265 ret[nbns] = NULL;
5266 }
Owen Taylor3473f882001-02-23 17:55:21 +00005267
Daniel Veillard77044732001-06-29 21:31:07 +00005268 cur = cur->next;
5269 }
5270 }
5271 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005272 }
Daniel Veillard77044732001-06-29 21:31:07 +00005273 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005274}
Daniel Veillard652327a2003-09-29 18:02:38 +00005275#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005276
5277/**
5278 * xmlSearchNs:
5279 * @doc: the document
5280 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005281 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005282 *
5283 * Search a Ns registered under a given name space for a document.
5284 * recurse on the parents until it finds the defined namespace
5285 * or return NULL otherwise.
5286 * @nameSpace can be NULL, this is a search for the default namespace.
5287 * We don't allow to cross entities boundaries. If you don't declare
5288 * the namespace within those you will be in troubles !!! A warning
5289 * is generated to cover this case.
5290 *
5291 * Returns the namespace pointer or NULL.
5292 */
5293xmlNsPtr
5294xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005295
Owen Taylor3473f882001-02-23 17:55:21 +00005296 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005297 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005298
5299 if (node == NULL) return(NULL);
5300 if ((nameSpace != NULL) &&
5301 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005302 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5303 /*
5304 * The XML-1.0 namespace is normally held on the root
5305 * element. In this case exceptionally create it on the
5306 * node element.
5307 */
5308 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5309 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005310 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005311 return(NULL);
5312 }
5313 memset(cur, 0, sizeof(xmlNs));
5314 cur->type = XML_LOCAL_NAMESPACE;
5315 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5316 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5317 cur->next = node->nsDef;
5318 node->nsDef = cur;
5319 return(cur);
5320 }
Owen Taylor3473f882001-02-23 17:55:21 +00005321 if (doc->oldNs == NULL) {
5322 /*
5323 * Allocate a new Namespace and fill the fields.
5324 */
5325 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5326 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005327 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005328 return(NULL);
5329 }
5330 memset(doc->oldNs, 0, sizeof(xmlNs));
5331 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5332
5333 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5334 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5335 }
5336 return(doc->oldNs);
5337 }
5338 while (node != NULL) {
5339 if ((node->type == XML_ENTITY_REF_NODE) ||
5340 (node->type == XML_ENTITY_NODE) ||
5341 (node->type == XML_ENTITY_DECL))
5342 return(NULL);
5343 if (node->type == XML_ELEMENT_NODE) {
5344 cur = node->nsDef;
5345 while (cur != NULL) {
5346 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5347 (cur->href != NULL))
5348 return(cur);
5349 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5350 (cur->href != NULL) &&
5351 (xmlStrEqual(cur->prefix, nameSpace)))
5352 return(cur);
5353 cur = cur->next;
5354 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005355 if (orig != node) {
5356 cur = node->ns;
5357 if (cur != NULL) {
5358 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5359 (cur->href != NULL))
5360 return(cur);
5361 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5362 (cur->href != NULL) &&
5363 (xmlStrEqual(cur->prefix, nameSpace)))
5364 return(cur);
5365 }
5366 }
Owen Taylor3473f882001-02-23 17:55:21 +00005367 }
5368 node = node->parent;
5369 }
5370 return(NULL);
5371}
5372
5373/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005374 * xmlNsInScope:
5375 * @doc: the document
5376 * @node: the current node
5377 * @ancestor: the ancestor carrying the namespace
5378 * @prefix: the namespace prefix
5379 *
5380 * Verify that the given namespace held on @ancestor is still in scope
5381 * on node.
5382 *
5383 * Returns 1 if true, 0 if false and -1 in case of error.
5384 */
5385static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005386xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5387 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005388{
5389 xmlNsPtr tst;
5390
5391 while ((node != NULL) && (node != ancestor)) {
5392 if ((node->type == XML_ENTITY_REF_NODE) ||
5393 (node->type == XML_ENTITY_NODE) ||
5394 (node->type == XML_ENTITY_DECL))
5395 return (-1);
5396 if (node->type == XML_ELEMENT_NODE) {
5397 tst = node->nsDef;
5398 while (tst != NULL) {
5399 if ((tst->prefix == NULL)
5400 && (prefix == NULL))
5401 return (0);
5402 if ((tst->prefix != NULL)
5403 && (prefix != NULL)
5404 && (xmlStrEqual(tst->prefix, prefix)))
5405 return (0);
5406 tst = tst->next;
5407 }
5408 }
5409 node = node->parent;
5410 }
5411 if (node != ancestor)
5412 return (-1);
5413 return (1);
5414}
5415
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005416/**
Owen Taylor3473f882001-02-23 17:55:21 +00005417 * xmlSearchNsByHref:
5418 * @doc: the document
5419 * @node: the current node
5420 * @href: the namespace value
5421 *
5422 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5423 * the defined namespace or return NULL otherwise.
5424 * Returns the namespace pointer or NULL.
5425 */
5426xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005427xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5428{
Owen Taylor3473f882001-02-23 17:55:21 +00005429 xmlNsPtr cur;
5430 xmlNodePtr orig = node;
5431
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005432 if ((node == NULL) || (href == NULL))
5433 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005434 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005435 /*
5436 * Only the document can hold the XML spec namespace.
5437 */
5438 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5439 /*
5440 * The XML-1.0 namespace is normally held on the root
5441 * element. In this case exceptionally create it on the
5442 * node element.
5443 */
5444 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5445 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005446 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005447 return (NULL);
5448 }
5449 memset(cur, 0, sizeof(xmlNs));
5450 cur->type = XML_LOCAL_NAMESPACE;
5451 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5452 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5453 cur->next = node->nsDef;
5454 node->nsDef = cur;
5455 return (cur);
5456 }
5457 if (doc->oldNs == NULL) {
5458 /*
5459 * Allocate a new Namespace and fill the fields.
5460 */
5461 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5462 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005463 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005464 return (NULL);
5465 }
5466 memset(doc->oldNs, 0, sizeof(xmlNs));
5467 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005468
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005469 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5470 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5471 }
5472 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005473 }
5474 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005475 if ((node->type == XML_ENTITY_REF_NODE) ||
5476 (node->type == XML_ENTITY_NODE) ||
5477 (node->type == XML_ENTITY_DECL))
5478 return (NULL);
5479 if (node->type == XML_ELEMENT_NODE) {
5480 cur = node->nsDef;
5481 while (cur != NULL) {
5482 if ((cur->href != NULL) && (href != NULL) &&
5483 (xmlStrEqual(cur->href, href))) {
5484 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5485 return (cur);
5486 }
5487 cur = cur->next;
5488 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005489 if (orig != node) {
5490 cur = node->ns;
5491 if (cur != NULL) {
5492 if ((cur->href != NULL) && (href != NULL) &&
5493 (xmlStrEqual(cur->href, href))) {
5494 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5495 return (cur);
5496 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005497 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005498 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005499 }
5500 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005501 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005502 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005503}
5504
5505/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005506 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005507 * @doc: the document
5508 * @tree: a node expected to hold the new namespace
5509 * @ns: the original namespace
5510 *
5511 * This function tries to locate a namespace definition in a tree
5512 * ancestors, or create a new namespace definition node similar to
5513 * @ns trying to reuse the same prefix. However if the given prefix is
5514 * null (default namespace) or reused within the subtree defined by
5515 * @tree or on one of its ancestors then a new prefix is generated.
5516 * Returns the (new) namespace definition or NULL in case of error
5517 */
5518xmlNsPtr
5519xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5520 xmlNsPtr def;
5521 xmlChar prefix[50];
5522 int counter = 1;
5523
5524 if (tree == NULL) {
5525#ifdef DEBUG_TREE
5526 xmlGenericError(xmlGenericErrorContext,
5527 "xmlNewReconciliedNs : tree == NULL\n");
5528#endif
5529 return(NULL);
5530 }
5531 if (ns == NULL) {
5532#ifdef DEBUG_TREE
5533 xmlGenericError(xmlGenericErrorContext,
5534 "xmlNewReconciliedNs : ns == NULL\n");
5535#endif
5536 return(NULL);
5537 }
5538 /*
5539 * Search an existing namespace definition inherited.
5540 */
5541 def = xmlSearchNsByHref(doc, tree, ns->href);
5542 if (def != NULL)
5543 return(def);
5544
5545 /*
5546 * Find a close prefix which is not already in use.
5547 * Let's strip namespace prefixes longer than 20 chars !
5548 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005549 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005550 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005551 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005552 snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005553
Owen Taylor3473f882001-02-23 17:55:21 +00005554 def = xmlSearchNs(doc, tree, prefix);
5555 while (def != NULL) {
5556 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005557 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005558 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005559 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005560 snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005561 def = xmlSearchNs(doc, tree, prefix);
5562 }
5563
5564 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005565 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005566 */
5567 def = xmlNewNs(tree, ns->href, prefix);
5568 return(def);
5569}
5570
Daniel Veillard652327a2003-09-29 18:02:38 +00005571#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005572/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005573 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005574 * @doc: the document
5575 * @tree: a node defining the subtree to reconciliate
5576 *
5577 * This function checks that all the namespaces declared within the given
5578 * tree are properly declared. This is needed for example after Copy or Cut
5579 * and then paste operations. The subtree may still hold pointers to
5580 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005581 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005582 * the new environment. If not possible the new namespaces are redeclared
5583 * on @tree at the top of the given subtree.
5584 * Returns the number of namespace declarations created or -1 in case of error.
5585 */
5586int
5587xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5588 xmlNsPtr *oldNs = NULL;
5589 xmlNsPtr *newNs = NULL;
5590 int sizeCache = 0;
5591 int nbCache = 0;
5592
5593 xmlNsPtr n;
5594 xmlNodePtr node = tree;
5595 xmlAttrPtr attr;
5596 int ret = 0, i;
5597
5598 while (node != NULL) {
5599 /*
5600 * Reconciliate the node namespace
5601 */
5602 if (node->ns != NULL) {
5603 /*
5604 * initialize the cache if needed
5605 */
5606 if (sizeCache == 0) {
5607 sizeCache = 10;
5608 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5609 sizeof(xmlNsPtr));
5610 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005611 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005612 return(-1);
5613 }
5614 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5615 sizeof(xmlNsPtr));
5616 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005617 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005618 xmlFree(oldNs);
5619 return(-1);
5620 }
5621 }
5622 for (i = 0;i < nbCache;i++) {
5623 if (oldNs[i] == node->ns) {
5624 node->ns = newNs[i];
5625 break;
5626 }
5627 }
5628 if (i == nbCache) {
5629 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005630 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005631 */
5632 n = xmlNewReconciliedNs(doc, tree, node->ns);
5633 if (n != NULL) { /* :-( what if else ??? */
5634 /*
5635 * check if we need to grow the cache buffers.
5636 */
5637 if (sizeCache <= nbCache) {
5638 sizeCache *= 2;
5639 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5640 sizeof(xmlNsPtr));
5641 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005642 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005643 xmlFree(newNs);
5644 return(-1);
5645 }
5646 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5647 sizeof(xmlNsPtr));
5648 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005649 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005650 xmlFree(oldNs);
5651 return(-1);
5652 }
5653 }
5654 newNs[nbCache] = n;
5655 oldNs[nbCache++] = node->ns;
5656 node->ns = n;
5657 }
5658 }
5659 }
5660 /*
5661 * now check for namespace hold by attributes on the node.
5662 */
5663 attr = node->properties;
5664 while (attr != NULL) {
5665 if (attr->ns != NULL) {
5666 /*
5667 * initialize the cache if needed
5668 */
5669 if (sizeCache == 0) {
5670 sizeCache = 10;
5671 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5672 sizeof(xmlNsPtr));
5673 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005674 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005675 return(-1);
5676 }
5677 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5678 sizeof(xmlNsPtr));
5679 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005680 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005681 xmlFree(oldNs);
5682 return(-1);
5683 }
5684 }
5685 for (i = 0;i < nbCache;i++) {
5686 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005687 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005688 break;
5689 }
5690 }
5691 if (i == nbCache) {
5692 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005693 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005694 */
5695 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5696 if (n != NULL) { /* :-( what if else ??? */
5697 /*
5698 * check if we need to grow the cache buffers.
5699 */
5700 if (sizeCache <= nbCache) {
5701 sizeCache *= 2;
5702 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5703 sizeof(xmlNsPtr));
5704 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005705 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005706 xmlFree(newNs);
5707 return(-1);
5708 }
5709 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5710 sizeof(xmlNsPtr));
5711 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005712 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005713 xmlFree(oldNs);
5714 return(-1);
5715 }
5716 }
5717 newNs[nbCache] = n;
5718 oldNs[nbCache++] = attr->ns;
5719 attr->ns = n;
5720 }
5721 }
5722 }
5723 attr = attr->next;
5724 }
5725
5726 /*
5727 * Browse the full subtree, deep first
5728 */
5729 if (node->children != NULL) {
5730 /* deep first */
5731 node = node->children;
5732 } else if ((node != tree) && (node->next != NULL)) {
5733 /* then siblings */
5734 node = node->next;
5735 } else if (node != tree) {
5736 /* go up to parents->next if needed */
5737 while (node != tree) {
5738 if (node->parent != NULL)
5739 node = node->parent;
5740 if ((node != tree) && (node->next != NULL)) {
5741 node = node->next;
5742 break;
5743 }
5744 if (node->parent == NULL) {
5745 node = NULL;
5746 break;
5747 }
5748 }
5749 /* exit condition */
5750 if (node == tree)
5751 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005752 } else
5753 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005754 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005755 if (oldNs != NULL)
5756 xmlFree(oldNs);
5757 if (newNs != NULL)
5758 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005759 return(ret);
5760}
Daniel Veillard652327a2003-09-29 18:02:38 +00005761#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005762
5763/**
5764 * xmlHasProp:
5765 * @node: the node
5766 * @name: the attribute name
5767 *
5768 * Search an attribute associated to a node
5769 * This function also looks in DTD attribute declaration for #FIXED or
5770 * default declaration values unless DTD use has been turned off.
5771 *
5772 * Returns the attribute or the attribute declaration or NULL if
5773 * neither was found.
5774 */
5775xmlAttrPtr
5776xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5777 xmlAttrPtr prop;
5778 xmlDocPtr doc;
5779
5780 if ((node == NULL) || (name == NULL)) return(NULL);
5781 /*
5782 * Check on the properties attached to the node
5783 */
5784 prop = node->properties;
5785 while (prop != NULL) {
5786 if (xmlStrEqual(prop->name, name)) {
5787 return(prop);
5788 }
5789 prop = prop->next;
5790 }
5791 if (!xmlCheckDTD) return(NULL);
5792
5793 /*
5794 * Check if there is a default declaration in the internal
5795 * or external subsets
5796 */
5797 doc = node->doc;
5798 if (doc != NULL) {
5799 xmlAttributePtr attrDecl;
5800 if (doc->intSubset != NULL) {
5801 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5802 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5803 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005804 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5805 /* return attribute declaration only if a default value is given
5806 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005807 return((xmlAttrPtr) attrDecl);
5808 }
5809 }
5810 return(NULL);
5811}
5812
5813/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005814 * xmlHasNsProp:
5815 * @node: the node
5816 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005817 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005818 *
5819 * Search for an attribute associated to a node
5820 * This attribute has to be anchored in the namespace specified.
5821 * This does the entity substitution.
5822 * This function looks in DTD attribute declaration for #FIXED or
5823 * default declaration values unless DTD use has been turned off.
5824 *
5825 * Returns the attribute or the attribute declaration or NULL
5826 * if neither was found.
5827 */
5828xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005829xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005830 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005831#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005832 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005833#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005834
5835 if (node == NULL)
5836 return(NULL);
5837
5838 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005839 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00005840 return(xmlHasProp(node, name));
5841 while (prop != NULL) {
5842 /*
5843 * One need to have
5844 * - same attribute names
5845 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005846 */
5847 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00005848 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
5849 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005850 }
5851 prop = prop->next;
5852 }
5853 if (!xmlCheckDTD) return(NULL);
5854
Daniel Veillard652327a2003-09-29 18:02:38 +00005855#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005856 /*
5857 * Check if there is a default declaration in the internal
5858 * or external subsets
5859 */
5860 doc = node->doc;
5861 if (doc != NULL) {
5862 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005863 xmlAttributePtr attrDecl = NULL;
5864 xmlNsPtr *nsList, *cur;
5865 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005866
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005867 nsList = xmlGetNsList(node->doc, node);
5868 if (nsList == NULL)
5869 return(NULL);
5870 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5871 ename = xmlStrdup(node->ns->prefix);
5872 ename = xmlStrcat(ename, BAD_CAST ":");
5873 ename = xmlStrcat(ename, node->name);
5874 } else {
5875 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005876 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005877 if (ename == NULL) {
5878 xmlFree(nsList);
5879 return(NULL);
5880 }
5881
5882 cur = nsList;
5883 while (*cur != NULL) {
5884 if (xmlStrEqual((*cur)->href, nameSpace)) {
5885 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
5886 name, (*cur)->prefix);
5887 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5888 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
5889 name, (*cur)->prefix);
5890 }
5891 cur++;
5892 }
5893 xmlFree(nsList);
5894 xmlFree(ename);
5895 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005896 }
5897 }
Daniel Veillard652327a2003-09-29 18:02:38 +00005898#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005899 return(NULL);
5900}
5901
5902/**
Owen Taylor3473f882001-02-23 17:55:21 +00005903 * xmlGetProp:
5904 * @node: the node
5905 * @name: the attribute name
5906 *
5907 * Search and get the value of an attribute associated to a node
5908 * This does the entity substitution.
5909 * This function looks in DTD attribute declaration for #FIXED or
5910 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00005911 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00005912 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
5913 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00005914 *
5915 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005916 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005917 */
5918xmlChar *
5919xmlGetProp(xmlNodePtr node, const xmlChar *name) {
5920 xmlAttrPtr prop;
5921 xmlDocPtr doc;
5922
5923 if ((node == NULL) || (name == NULL)) return(NULL);
5924 /*
5925 * Check on the properties attached to the node
5926 */
5927 prop = node->properties;
5928 while (prop != NULL) {
5929 if (xmlStrEqual(prop->name, name)) {
5930 xmlChar *ret;
5931
5932 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5933 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5934 return(ret);
5935 }
5936 prop = prop->next;
5937 }
5938 if (!xmlCheckDTD) return(NULL);
5939
5940 /*
5941 * Check if there is a default declaration in the internal
5942 * or external subsets
5943 */
5944 doc = node->doc;
5945 if (doc != NULL) {
5946 xmlAttributePtr attrDecl;
5947 if (doc->intSubset != NULL) {
5948 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5949 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5950 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005951 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5952 /* return attribute declaration only if a default value is given
5953 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005954 return(xmlStrdup(attrDecl->defaultValue));
5955 }
5956 }
5957 return(NULL);
5958}
5959
5960/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005961 * xmlGetNoNsProp:
5962 * @node: the node
5963 * @name: the attribute name
5964 *
5965 * Search and get the value of an attribute associated to a node
5966 * This does the entity substitution.
5967 * This function looks in DTD attribute declaration for #FIXED or
5968 * default declaration values unless DTD use has been turned off.
5969 * This function is similar to xmlGetProp except it will accept only
5970 * an attribute in no namespace.
5971 *
5972 * Returns the attribute value or NULL if not found.
5973 * It's up to the caller to free the memory with xmlFree().
5974 */
5975xmlChar *
5976xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
5977 xmlAttrPtr prop;
5978 xmlDocPtr doc;
5979
5980 if ((node == NULL) || (name == NULL)) return(NULL);
5981 /*
5982 * Check on the properties attached to the node
5983 */
5984 prop = node->properties;
5985 while (prop != NULL) {
5986 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
5987 xmlChar *ret;
5988
5989 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5990 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5991 return(ret);
5992 }
5993 prop = prop->next;
5994 }
5995 if (!xmlCheckDTD) return(NULL);
5996
5997 /*
5998 * Check if there is a default declaration in the internal
5999 * or external subsets
6000 */
6001 doc = node->doc;
6002 if (doc != NULL) {
6003 xmlAttributePtr attrDecl;
6004 if (doc->intSubset != NULL) {
6005 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6006 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6007 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006008 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6009 /* return attribute declaration only if a default value is given
6010 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00006011 return(xmlStrdup(attrDecl->defaultValue));
6012 }
6013 }
6014 return(NULL);
6015}
6016
6017/**
Owen Taylor3473f882001-02-23 17:55:21 +00006018 * xmlGetNsProp:
6019 * @node: the node
6020 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006021 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006022 *
6023 * Search and get the value of an attribute associated to a node
6024 * This attribute has to be anchored in the namespace specified.
6025 * This does the entity substitution.
6026 * This function looks in DTD attribute declaration for #FIXED or
6027 * default declaration values unless DTD use has been turned off.
6028 *
6029 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006030 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006031 */
6032xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006033xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006034 xmlAttrPtr prop;
6035 xmlDocPtr doc;
6036 xmlNsPtr ns;
6037
6038 if (node == NULL)
6039 return(NULL);
6040
6041 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00006042 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00006043 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00006044 while (prop != NULL) {
6045 /*
6046 * One need to have
6047 * - same attribute names
6048 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006049 */
6050 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00006051 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00006052 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006053 xmlChar *ret;
6054
6055 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6056 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6057 return(ret);
6058 }
6059 prop = prop->next;
6060 }
6061 if (!xmlCheckDTD) return(NULL);
6062
6063 /*
6064 * Check if there is a default declaration in the internal
6065 * or external subsets
6066 */
6067 doc = node->doc;
6068 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006069 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00006070 xmlAttributePtr attrDecl;
6071
Owen Taylor3473f882001-02-23 17:55:21 +00006072 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6073 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6074 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6075
6076 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6077 /*
6078 * The DTD declaration only allows a prefix search
6079 */
6080 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006081 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006082 return(xmlStrdup(attrDecl->defaultValue));
6083 }
6084 }
6085 }
6086 return(NULL);
6087}
6088
Daniel Veillard652327a2003-09-29 18:02:38 +00006089#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006090/**
6091 * xmlSetProp:
6092 * @node: the node
6093 * @name: the attribute name
6094 * @value: the attribute value
6095 *
6096 * Set (or reset) an attribute carried by a node.
6097 * Returns the attribute pointer.
6098 */
6099xmlAttrPtr
6100xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006101 xmlAttrPtr prop;
6102 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006103
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006104 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006105 return(NULL);
6106 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006107 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006108 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006109 if ((xmlStrEqual(prop->name, name)) &&
6110 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006111 xmlNodePtr oldprop = prop->children;
6112
Owen Taylor3473f882001-02-23 17:55:21 +00006113 prop->children = NULL;
6114 prop->last = NULL;
6115 if (value != NULL) {
6116 xmlChar *buffer;
6117 xmlNodePtr tmp;
6118
6119 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6120 prop->children = xmlStringGetNodeList(node->doc, buffer);
6121 prop->last = NULL;
6122 prop->doc = doc;
6123 tmp = prop->children;
6124 while (tmp != NULL) {
6125 tmp->parent = (xmlNodePtr) prop;
6126 tmp->doc = doc;
6127 if (tmp->next == NULL)
6128 prop->last = tmp;
6129 tmp = tmp->next;
6130 }
6131 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006132 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006133 if (oldprop != NULL)
6134 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006135 return(prop);
6136 }
6137 prop = prop->next;
6138 }
6139 prop = xmlNewProp(node, name, value);
6140 return(prop);
6141}
6142
6143/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006144 * xmlUnsetProp:
6145 * @node: the node
6146 * @name: the attribute name
6147 *
6148 * Remove an attribute carried by a node.
6149 * Returns 0 if successful, -1 if not found
6150 */
6151int
6152xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard814a76d2003-01-23 18:24:20 +00006153 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard75bea542001-05-11 17:41:21 +00006154
6155 if ((node == NULL) || (name == NULL))
6156 return(-1);
Daniel Veillard814a76d2003-01-23 18:24:20 +00006157 prop = node->properties;
Daniel Veillard75bea542001-05-11 17:41:21 +00006158 while (prop != NULL) {
6159 if ((xmlStrEqual(prop->name, name)) &&
6160 (prop->ns == NULL)) {
6161 if (prev == NULL)
6162 node->properties = prop->next;
6163 else
6164 prev->next = prop->next;
6165 xmlFreeProp(prop);
6166 return(0);
6167 }
6168 prev = prop;
6169 prop = prop->next;
6170 }
6171 return(-1);
6172}
6173
6174/**
Owen Taylor3473f882001-02-23 17:55:21 +00006175 * xmlSetNsProp:
6176 * @node: the node
6177 * @ns: the namespace definition
6178 * @name: the attribute name
6179 * @value: the attribute value
6180 *
6181 * Set (or reset) an attribute carried by a node.
6182 * The ns structure must be in scope, this is not checked.
6183 *
6184 * Returns the attribute pointer.
6185 */
6186xmlAttrPtr
6187xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6188 const xmlChar *value) {
6189 xmlAttrPtr prop;
6190
6191 if ((node == NULL) || (name == NULL))
6192 return(NULL);
6193
6194 if (ns == NULL)
6195 return(xmlSetProp(node, name, value));
6196 if (ns->href == NULL)
6197 return(NULL);
6198 prop = node->properties;
6199
6200 while (prop != NULL) {
6201 /*
6202 * One need to have
6203 * - same attribute names
6204 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006205 */
6206 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006207 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006208 if (prop->children != NULL)
6209 xmlFreeNodeList(prop->children);
6210 prop->children = NULL;
6211 prop->last = NULL;
6212 prop->ns = ns;
6213 if (value != NULL) {
6214 xmlChar *buffer;
6215 xmlNodePtr tmp;
6216
6217 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6218 prop->children = xmlStringGetNodeList(node->doc, buffer);
6219 prop->last = NULL;
6220 tmp = prop->children;
6221 while (tmp != NULL) {
6222 tmp->parent = (xmlNodePtr) prop;
6223 if (tmp->next == NULL)
6224 prop->last = tmp;
6225 tmp = tmp->next;
6226 }
6227 xmlFree(buffer);
6228 }
6229 return(prop);
6230 }
6231 prop = prop->next;
6232 }
6233 prop = xmlNewNsProp(node, ns, name, value);
6234 return(prop);
6235}
6236
6237/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006238 * xmlUnsetNsProp:
6239 * @node: the node
6240 * @ns: the namespace definition
6241 * @name: the attribute name
6242 *
6243 * Remove an attribute carried by a node.
6244 * Returns 0 if successful, -1 if not found
6245 */
6246int
6247xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6248 xmlAttrPtr prop = node->properties, prev = NULL;;
6249
6250 if ((node == NULL) || (name == NULL))
6251 return(-1);
6252 if (ns == NULL)
6253 return(xmlUnsetProp(node, name));
6254 if (ns->href == NULL)
6255 return(-1);
6256 while (prop != NULL) {
6257 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillard0bf29002002-08-01 12:54:11 +00006258 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006259 if (prev == NULL)
6260 node->properties = prop->next;
6261 else
6262 prev->next = prop->next;
6263 xmlFreeProp(prop);
6264 return(0);
6265 }
6266 prev = prop;
6267 prop = prop->next;
6268 }
6269 return(-1);
6270}
Daniel Veillard652327a2003-09-29 18:02:38 +00006271#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006272
6273/**
Owen Taylor3473f882001-02-23 17:55:21 +00006274 * xmlNodeIsText:
6275 * @node: the node
6276 *
6277 * Is this node a Text node ?
6278 * Returns 1 yes, 0 no
6279 */
6280int
6281xmlNodeIsText(xmlNodePtr node) {
6282 if (node == NULL) return(0);
6283
6284 if (node->type == XML_TEXT_NODE) return(1);
6285 return(0);
6286}
6287
6288/**
6289 * xmlIsBlankNode:
6290 * @node: the node
6291 *
6292 * Checks whether this node is an empty or whitespace only
6293 * (and possibly ignorable) text-node.
6294 *
6295 * Returns 1 yes, 0 no
6296 */
6297int
6298xmlIsBlankNode(xmlNodePtr node) {
6299 const xmlChar *cur;
6300 if (node == NULL) return(0);
6301
Daniel Veillard7db37732001-07-12 01:20:08 +00006302 if ((node->type != XML_TEXT_NODE) &&
6303 (node->type != XML_CDATA_SECTION_NODE))
6304 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006305 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006306 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006307 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006308 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006309 cur++;
6310 }
6311
6312 return(1);
6313}
6314
6315/**
6316 * xmlTextConcat:
6317 * @node: the node
6318 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006319 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006320 *
6321 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006322 *
6323 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006324 */
6325
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006326int
Owen Taylor3473f882001-02-23 17:55:21 +00006327xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006328 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006329
6330 if ((node->type != XML_TEXT_NODE) &&
6331 (node->type != XML_CDATA_SECTION_NODE)) {
6332#ifdef DEBUG_TREE
6333 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006334 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006335#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006336 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006337 }
Owen Taylor3473f882001-02-23 17:55:21 +00006338 node->content = xmlStrncat(node->content, content, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006339 if (node->content == NULL)
6340 return(-1);
6341 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006342}
6343
6344/************************************************************************
6345 * *
6346 * Output : to a FILE or in memory *
6347 * *
6348 ************************************************************************/
6349
Owen Taylor3473f882001-02-23 17:55:21 +00006350/**
6351 * xmlBufferCreate:
6352 *
6353 * routine to create an XML buffer.
6354 * returns the new structure.
6355 */
6356xmlBufferPtr
6357xmlBufferCreate(void) {
6358 xmlBufferPtr ret;
6359
6360 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6361 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006362 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006363 return(NULL);
6364 }
6365 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006366 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006367 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006368 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006369 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006370 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006371 xmlFree(ret);
6372 return(NULL);
6373 }
6374 ret->content[0] = 0;
6375 return(ret);
6376}
6377
6378/**
6379 * xmlBufferCreateSize:
6380 * @size: initial size of buffer
6381 *
6382 * routine to create an XML buffer.
6383 * returns the new structure.
6384 */
6385xmlBufferPtr
6386xmlBufferCreateSize(size_t size) {
6387 xmlBufferPtr ret;
6388
6389 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6390 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006391 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006392 return(NULL);
6393 }
6394 ret->use = 0;
6395 ret->alloc = xmlBufferAllocScheme;
6396 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6397 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006398 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006399 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006400 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006401 xmlFree(ret);
6402 return(NULL);
6403 }
6404 ret->content[0] = 0;
6405 } else
6406 ret->content = NULL;
6407 return(ret);
6408}
6409
6410/**
Daniel Veillard53350552003-09-18 13:35:51 +00006411 * xmlBufferCreateStatic:
6412 * @mem: the memory area
6413 * @size: the size in byte
6414 *
6415 * routine to create an XML buffer from an immutable memory area,
6416 * The are won't be modified nor copied, and is expected to be
6417 * present until the end of the buffer lifetime.
6418 *
6419 * returns the new structure.
6420 */
6421xmlBufferPtr
6422xmlBufferCreateStatic(void *mem, size_t size) {
6423 xmlBufferPtr ret;
6424
6425 if ((mem == NULL) || (size == 0))
6426 return(NULL);
6427
6428 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6429 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006430 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006431 return(NULL);
6432 }
6433 ret->use = size;
6434 ret->size = size;
6435 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6436 ret->content = (xmlChar *) mem;
6437 return(ret);
6438}
6439
6440/**
Owen Taylor3473f882001-02-23 17:55:21 +00006441 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006442 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006443 * @scheme: allocation scheme to use
6444 *
6445 * Sets the allocation scheme for this buffer
6446 */
6447void
6448xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6449 xmlBufferAllocationScheme scheme) {
6450 if (buf == NULL) {
6451#ifdef DEBUG_BUFFER
6452 xmlGenericError(xmlGenericErrorContext,
6453 "xmlBufferSetAllocationScheme: buf == NULL\n");
6454#endif
6455 return;
6456 }
Daniel Veillard53350552003-09-18 13:35:51 +00006457 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006458
6459 buf->alloc = scheme;
6460}
6461
6462/**
6463 * xmlBufferFree:
6464 * @buf: the buffer to free
6465 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006466 * Frees an XML buffer. It frees both the content and the structure which
6467 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006468 */
6469void
6470xmlBufferFree(xmlBufferPtr buf) {
6471 if (buf == NULL) {
6472#ifdef DEBUG_BUFFER
6473 xmlGenericError(xmlGenericErrorContext,
6474 "xmlBufferFree: buf == NULL\n");
6475#endif
6476 return;
6477 }
Daniel Veillard53350552003-09-18 13:35:51 +00006478
6479 if ((buf->content != NULL) &&
6480 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006481 xmlFree(buf->content);
6482 }
Owen Taylor3473f882001-02-23 17:55:21 +00006483 xmlFree(buf);
6484}
6485
6486/**
6487 * xmlBufferEmpty:
6488 * @buf: the buffer
6489 *
6490 * empty a buffer.
6491 */
6492void
6493xmlBufferEmpty(xmlBufferPtr buf) {
6494 if (buf->content == NULL) return;
6495 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006496 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006497 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006498 } else {
6499 memset(buf->content, 0, buf->size);
6500 }
Owen Taylor3473f882001-02-23 17:55:21 +00006501}
6502
6503/**
6504 * xmlBufferShrink:
6505 * @buf: the buffer to dump
6506 * @len: the number of xmlChar to remove
6507 *
6508 * Remove the beginning of an XML buffer.
6509 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006510 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006511 */
6512int
6513xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
6514 if (len == 0) return(0);
6515 if (len > buf->use) return(-1);
6516
6517 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006518 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6519 buf->content += len;
6520 } else {
6521 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6522 buf->content[buf->use] = 0;
6523 }
Owen Taylor3473f882001-02-23 17:55:21 +00006524 return(len);
6525}
6526
6527/**
6528 * xmlBufferGrow:
6529 * @buf: the buffer
6530 * @len: the minimum free size to allocate
6531 *
6532 * Grow the available space of an XML buffer.
6533 *
6534 * Returns the new available space or -1 in case of error
6535 */
6536int
6537xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6538 int size;
6539 xmlChar *newbuf;
6540
Daniel Veillard53350552003-09-18 13:35:51 +00006541 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006542 if (len + buf->use < buf->size) return(0);
6543
6544 size = buf->use + len + 100;
6545
6546 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006547 if (newbuf == NULL) {
6548 xmlTreeErrMemory("growing buffer");
6549 return(-1);
6550 }
Owen Taylor3473f882001-02-23 17:55:21 +00006551 buf->content = newbuf;
6552 buf->size = size;
6553 return(buf->size - buf->use);
6554}
6555
6556/**
6557 * xmlBufferDump:
6558 * @file: the file output
6559 * @buf: the buffer to dump
6560 *
6561 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006562 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006563 */
6564int
6565xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6566 int ret;
6567
6568 if (buf == NULL) {
6569#ifdef DEBUG_BUFFER
6570 xmlGenericError(xmlGenericErrorContext,
6571 "xmlBufferDump: buf == NULL\n");
6572#endif
6573 return(0);
6574 }
6575 if (buf->content == NULL) {
6576#ifdef DEBUG_BUFFER
6577 xmlGenericError(xmlGenericErrorContext,
6578 "xmlBufferDump: buf->content == NULL\n");
6579#endif
6580 return(0);
6581 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006582 if (file == NULL)
6583 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006584 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6585 return(ret);
6586}
6587
6588/**
6589 * xmlBufferContent:
6590 * @buf: the buffer
6591 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006592 * Function to extract the content of a buffer
6593 *
Owen Taylor3473f882001-02-23 17:55:21 +00006594 * Returns the internal content
6595 */
6596
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006597const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006598xmlBufferContent(const xmlBufferPtr buf)
6599{
6600 if(!buf)
6601 return NULL;
6602
6603 return buf->content;
6604}
6605
6606/**
6607 * xmlBufferLength:
6608 * @buf: the buffer
6609 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006610 * Function to get the length of a buffer
6611 *
Owen Taylor3473f882001-02-23 17:55:21 +00006612 * Returns the length of data in the internal content
6613 */
6614
6615int
6616xmlBufferLength(const xmlBufferPtr buf)
6617{
6618 if(!buf)
6619 return 0;
6620
6621 return buf->use;
6622}
6623
6624/**
6625 * xmlBufferResize:
6626 * @buf: the buffer to resize
6627 * @size: the desired size
6628 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006629 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006630 *
6631 * Returns 0 in case of problems, 1 otherwise
6632 */
6633int
6634xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6635{
6636 unsigned int newSize;
6637 xmlChar* rebuf = NULL;
6638
Daniel Veillard53350552003-09-18 13:35:51 +00006639 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6640
Owen Taylor3473f882001-02-23 17:55:21 +00006641 /*take care of empty case*/
6642 newSize = (buf->size ? buf->size*2 : size);
6643
6644 /* Don't resize if we don't have to */
6645 if (size < buf->size)
6646 return 1;
6647
6648 /* figure out new size */
6649 switch (buf->alloc){
6650 case XML_BUFFER_ALLOC_DOUBLEIT:
6651 while (size > newSize) newSize *= 2;
6652 break;
6653 case XML_BUFFER_ALLOC_EXACT:
6654 newSize = size+10;
6655 break;
6656 default:
6657 newSize = size+10;
6658 break;
6659 }
6660
6661 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006662 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006663 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006664 rebuf = (xmlChar *) xmlRealloc(buf->content,
6665 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006666 } else {
6667 /*
6668 * if we are reallocating a buffer far from being full, it's
6669 * better to make a new allocation and copy only the used range
6670 * and free the old one.
6671 */
6672 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6673 if (rebuf != NULL) {
6674 memcpy(rebuf, buf->content, buf->use);
6675 xmlFree(buf->content);
6676 }
Daniel Veillarde5984082003-08-19 22:21:13 +00006677 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006678 }
Owen Taylor3473f882001-02-23 17:55:21 +00006679 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006680 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006681 return 0;
6682 }
6683 buf->content = rebuf;
6684 buf->size = newSize;
6685
6686 return 1;
6687}
6688
6689/**
6690 * xmlBufferAdd:
6691 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006692 * @str: the #xmlChar string
6693 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006694 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006695 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006696 * str is recomputed.
6697 */
6698void
6699xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6700 unsigned int needSize;
6701
6702 if (str == NULL) {
6703#ifdef DEBUG_BUFFER
6704 xmlGenericError(xmlGenericErrorContext,
6705 "xmlBufferAdd: str == NULL\n");
6706#endif
6707 return;
6708 }
Daniel Veillard53350552003-09-18 13:35:51 +00006709 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006710 if (len < -1) {
6711#ifdef DEBUG_BUFFER
6712 xmlGenericError(xmlGenericErrorContext,
6713 "xmlBufferAdd: len < 0\n");
6714#endif
6715 return;
6716 }
6717 if (len == 0) return;
6718
6719 if (len < 0)
6720 len = xmlStrlen(str);
6721
6722 if (len <= 0) return;
6723
6724 needSize = buf->use + len + 2;
6725 if (needSize > buf->size){
6726 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006727 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006728 return;
6729 }
6730 }
6731
6732 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6733 buf->use += len;
6734 buf->content[buf->use] = 0;
6735}
6736
6737/**
6738 * xmlBufferAddHead:
6739 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006740 * @str: the #xmlChar string
6741 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006742 *
6743 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006744 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00006745 */
6746void
6747xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6748 unsigned int needSize;
6749
Daniel Veillard53350552003-09-18 13:35:51 +00006750 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006751 if (str == NULL) {
6752#ifdef DEBUG_BUFFER
6753 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006754 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006755#endif
6756 return;
6757 }
6758 if (len < -1) {
6759#ifdef DEBUG_BUFFER
6760 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006761 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006762#endif
6763 return;
6764 }
6765 if (len == 0) return;
6766
6767 if (len < 0)
6768 len = xmlStrlen(str);
6769
6770 if (len <= 0) return;
6771
6772 needSize = buf->use + len + 2;
6773 if (needSize > buf->size){
6774 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006775 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006776 return;
6777 }
6778 }
6779
6780 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6781 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6782 buf->use += len;
6783 buf->content[buf->use] = 0;
6784}
6785
6786/**
6787 * xmlBufferCat:
6788 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006789 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006790 *
6791 * Append a zero terminated string to an XML buffer.
6792 */
6793void
6794xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillard53350552003-09-18 13:35:51 +00006795 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006796 if (str != NULL)
6797 xmlBufferAdd(buf, str, -1);
6798}
6799
6800/**
6801 * xmlBufferCCat:
6802 * @buf: the buffer to dump
6803 * @str: the C char string
6804 *
6805 * Append a zero terminated C string to an XML buffer.
6806 */
6807void
6808xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6809 const char *cur;
6810
Daniel Veillard53350552003-09-18 13:35:51 +00006811 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006812 if (str == NULL) {
6813#ifdef DEBUG_BUFFER
6814 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006815 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006816#endif
6817 return;
6818 }
6819 for (cur = str;*cur != 0;cur++) {
6820 if (buf->use + 10 >= buf->size) {
6821 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006822 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006823 return;
6824 }
6825 }
6826 buf->content[buf->use++] = *cur;
6827 }
6828 buf->content[buf->use] = 0;
6829}
6830
6831/**
6832 * xmlBufferWriteCHAR:
6833 * @buf: the XML buffer
6834 * @string: the string to add
6835 *
6836 * routine which manages and grows an output buffer. This one adds
6837 * xmlChars at the end of the buffer.
6838 */
6839void
Daniel Veillard53350552003-09-18 13:35:51 +00006840xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
6841 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006842 xmlBufferCat(buf, string);
6843}
6844
6845/**
6846 * xmlBufferWriteChar:
6847 * @buf: the XML buffer output
6848 * @string: the string to add
6849 *
6850 * routine which manage and grows an output buffer. This one add
6851 * C chars at the end of the array.
6852 */
6853void
6854xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillard53350552003-09-18 13:35:51 +00006855 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006856 xmlBufferCCat(buf, string);
6857}
6858
6859
6860/**
6861 * xmlBufferWriteQuotedString:
6862 * @buf: the XML buffer output
6863 * @string: the string to add
6864 *
6865 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00006866 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00006867 * quote or double-quotes internally
6868 */
6869void
6870xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00006871 const xmlChar *cur, *base;
Daniel Veillard53350552003-09-18 13:35:51 +00006872 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00006873 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00006874 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00006875#ifdef DEBUG_BUFFER
6876 xmlGenericError(xmlGenericErrorContext,
6877 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
6878#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00006879 xmlBufferCCat(buf, "\"");
6880 base = cur = string;
6881 while(*cur != 0){
6882 if(*cur == '"'){
6883 if (base != cur)
6884 xmlBufferAdd(buf, base, cur - base);
6885 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
6886 cur++;
6887 base = cur;
6888 }
6889 else {
6890 cur++;
6891 }
6892 }
6893 if (base != cur)
6894 xmlBufferAdd(buf, base, cur - base);
6895 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00006896 }
Daniel Veillard39057f42003-08-04 01:33:43 +00006897 else{
6898 xmlBufferCCat(buf, "\'");
6899 xmlBufferCat(buf, string);
6900 xmlBufferCCat(buf, "\'");
6901 }
Owen Taylor3473f882001-02-23 17:55:21 +00006902 } else {
6903 xmlBufferCCat(buf, "\"");
6904 xmlBufferCat(buf, string);
6905 xmlBufferCCat(buf, "\"");
6906 }
6907}
6908
6909
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00006910#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006911/************************************************************************
6912 * *
Daniel Veillarde2238d52003-10-09 13:14:55 +00006913 * Output error handlers *
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006914 * *
6915 ************************************************************************/
6916/**
6917 * xmlSaveErrMemory:
6918 * @extra: extra informations
6919 *
6920 * Handle an out of memory condition
6921 */
6922static void
6923xmlSaveErrMemory(const char *extra)
6924{
6925 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
6926}
6927
6928/**
6929 * xmlSaveErr:
6930 * @code: the error number
6931 * @node: the location of the error.
6932 * @extra: extra informations
6933 *
6934 * Handle an out of memory condition
6935 */
6936static void
6937xmlSaveErr(int code, xmlNodePtr node, const char *extra)
6938{
6939 const char *msg = NULL;
6940
6941 switch(code) {
6942 case XML_SAVE_NOT_UTF8:
6943 msg = "string is not in UTF-8";
6944 break;
6945 case XML_SAVE_CHAR_INVALID:
6946 msg = "invalid character value";
6947 break;
6948 case XML_SAVE_UNKNOWN_ENCODING:
6949 msg = "unknown encoding %s";
6950 break;
Daniel Veillarde2238d52003-10-09 13:14:55 +00006951 case XML_SAVE_NO_DOCTYPE:
6952 msg = "document has no DOCTYPE";
6953 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006954 default:
6955 msg = "unexpected error number";
6956 }
Daniel Veillarde2238d52003-10-09 13:14:55 +00006957 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006958}
6959/************************************************************************
6960 * *
Owen Taylor3473f882001-02-23 17:55:21 +00006961 * Dumping XML tree content to a simple buffer *
6962 * *
6963 ************************************************************************/
6964
Owen Taylor3473f882001-02-23 17:55:21 +00006965/**
Daniel Veillarda6d05382002-02-13 13:07:41 +00006966 * xmlAttrSerializeContent:
6967 * @buf: the XML buffer output
6968 * @doc: the document
6969 * @attr: the attribute pointer
6970 *
6971 * Serialize the attribute in the buffer
6972 */
6973static void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006974xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr)
6975{
Daniel Veillarda6d05382002-02-13 13:07:41 +00006976 const xmlChar *cur, *base;
6977 xmlNodePtr children;
6978
6979 children = attr->children;
6980 while (children != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006981 switch (children->type) {
6982 case XML_TEXT_NODE:
6983 base = cur = children->content;
6984 while (*cur != 0) {
6985 if (*cur == '\n') {
6986 if (base != cur)
6987 xmlBufferAdd(buf, base, cur - base);
6988 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
6989 cur++;
6990 base = cur;
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006991 } else if (*cur == '\r') {
6992 if (base != cur)
6993 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00006994 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006995 cur++;
6996 base = cur;
6997 } else if (*cur == '\t') {
6998 if (base != cur)
6999 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00007000 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00007001 cur++;
7002 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007003#if 0
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007004 } else if (*cur == '\'') {
7005 if (base != cur)
7006 xmlBufferAdd(buf, base, cur - base);
7007 xmlBufferAdd(buf, BAD_CAST "&apos;", 6);
7008 cur++;
7009 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007010#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007011 } else if (*cur == '"') {
7012 if (base != cur)
7013 xmlBufferAdd(buf, base, cur - base);
7014 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7015 cur++;
7016 base = cur;
7017 } else if (*cur == '<') {
7018 if (base != cur)
7019 xmlBufferAdd(buf, base, cur - base);
7020 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
7021 cur++;
7022 base = cur;
7023 } else if (*cur == '>') {
7024 if (base != cur)
7025 xmlBufferAdd(buf, base, cur - base);
7026 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
7027 cur++;
7028 base = cur;
7029 } else if (*cur == '&') {
7030 if (base != cur)
7031 xmlBufferAdd(buf, base, cur - base);
7032 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
7033 cur++;
7034 base = cur;
7035 } else if ((*cur >= 0x80) && ((doc == NULL) ||
7036 (doc->encoding ==
7037 NULL))) {
7038 /*
7039 * We assume we have UTF-8 content.
7040 */
7041 char tmp[10];
7042 int val = 0, l = 1;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007043
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007044 if (base != cur)
7045 xmlBufferAdd(buf, base, cur - base);
7046 if (*cur < 0xC0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007047 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr,
7048 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007049 if (doc != NULL)
7050 doc->encoding =
7051 xmlStrdup(BAD_CAST "ISO-8859-1");
7052 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
7053 tmp[sizeof(tmp) - 1] = 0;
7054 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7055 cur++;
7056 base = cur;
7057 continue;
7058 } else if (*cur < 0xE0) {
7059 val = (cur[0]) & 0x1F;
7060 val <<= 6;
7061 val |= (cur[1]) & 0x3F;
7062 l = 2;
7063 } else if (*cur < 0xF0) {
7064 val = (cur[0]) & 0x0F;
7065 val <<= 6;
7066 val |= (cur[1]) & 0x3F;
7067 val <<= 6;
7068 val |= (cur[2]) & 0x3F;
7069 l = 3;
7070 } else if (*cur < 0xF8) {
7071 val = (cur[0]) & 0x07;
7072 val <<= 6;
7073 val |= (cur[1]) & 0x3F;
7074 val <<= 6;
7075 val |= (cur[2]) & 0x3F;
7076 val <<= 6;
7077 val |= (cur[3]) & 0x3F;
7078 l = 4;
7079 }
7080 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007081 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr,
7082 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007083 if (doc != NULL)
7084 doc->encoding =
7085 xmlStrdup(BAD_CAST "ISO-8859-1");
7086 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
7087 tmp[sizeof(tmp) - 1] = 0;
7088 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7089 cur++;
7090 base = cur;
7091 continue;
7092 }
7093 /*
7094 * We could do multiple things here. Just save
7095 * as a char ref
7096 */
7097 snprintf(tmp, sizeof(tmp), "&#x%X;", val);
7098 tmp[sizeof(tmp) - 1] = 0;
7099 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7100 cur += l;
7101 base = cur;
7102 } else {
7103 cur++;
7104 }
7105 }
7106 if (base != cur)
7107 xmlBufferAdd(buf, base, cur - base);
7108 break;
7109 case XML_ENTITY_REF_NODE:
7110 xmlBufferAdd(buf, BAD_CAST "&", 1);
7111 xmlBufferAdd(buf, children->name,
7112 xmlStrlen(children->name));
7113 xmlBufferAdd(buf, BAD_CAST ";", 1);
7114 break;
7115 default:
7116 /* should not happen unless we have a badly built tree */
7117 break;
7118 }
7119 children = children->next;
Owen Taylor3473f882001-02-23 17:55:21 +00007120 }
7121}
7122
7123/**
7124 * xmlNodeDump:
7125 * @buf: the XML buffer output
7126 * @doc: the document
7127 * @cur: the current node
7128 * @level: the imbrication level for indenting
7129 * @format: is formatting allowed
7130 *
7131 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007132 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007133 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007134 *
7135 * Returns the number of bytes written to the buffer or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00007136 */
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007137int
Owen Taylor3473f882001-02-23 17:55:21 +00007138xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007139 int format)
7140{
7141 unsigned int use;
7142 int ret;
7143 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007144
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007145 xmlInitParser();
7146
Owen Taylor3473f882001-02-23 17:55:21 +00007147 if (cur == NULL) {
7148#ifdef DEBUG_TREE
7149 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007150 "xmlNodeDump : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007151#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007152 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007153 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007154 if (buf == NULL) {
7155#ifdef DEBUG_TREE
7156 xmlGenericError(xmlGenericErrorContext,
7157 "xmlNodeDump : buf == NULL\n");
7158#endif
7159 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007160 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007161 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
7162 if (outbuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007163 xmlSaveErrMemory("creating buffer");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007164 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007165 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007166 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
7167 outbuf->buffer = buf;
7168 outbuf->encoder = NULL;
7169 outbuf->writecallback = NULL;
7170 outbuf->closecallback = NULL;
7171 outbuf->context = NULL;
7172 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007173
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007174 use = buf->use;
7175 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
7176 xmlFree(outbuf);
7177 ret = buf->use - use;
7178 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00007179}
7180
7181/**
7182 * xmlElemDump:
7183 * @f: the FILE * for the output
7184 * @doc: the document
7185 * @cur: the current node
7186 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007187 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00007188 */
7189void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007190xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
7191{
7192 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007193
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007194 xmlInitParser();
7195
Owen Taylor3473f882001-02-23 17:55:21 +00007196 if (cur == NULL) {
7197#ifdef DEBUG_TREE
7198 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007199 "xmlElemDump : cur == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007200#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007201 return;
Owen Taylor3473f882001-02-23 17:55:21 +00007202 }
Owen Taylor3473f882001-02-23 17:55:21 +00007203#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007204 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00007205 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007206 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007207 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007208#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007209
7210 outbuf = xmlOutputBufferCreateFile(f, NULL);
7211 if (outbuf == NULL)
7212 return;
7213 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007214#ifdef LIBXML_HTML_ENABLED
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007215 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
7216#else
Daniel Veillard2189b592003-10-21 00:08:42 +00007217 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007218#endif /* LIBXML_HTML_ENABLED */
7219 } else
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007220 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
7221 xmlOutputBufferClose(outbuf);
Owen Taylor3473f882001-02-23 17:55:21 +00007222}
7223
7224/************************************************************************
7225 * *
7226 * Dumping XML tree content to an I/O output buffer *
7227 * *
7228 ************************************************************************/
7229
Daniel Veillard4432df22003-09-28 18:58:27 +00007230#ifdef LIBXML_HTML_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007231static void
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007232xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7233 int level, int format, const char *encoding);
Daniel Veillard4432df22003-09-28 18:58:27 +00007234#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007235static void
Owen Taylor3473f882001-02-23 17:55:21 +00007236xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7237 int level, int format, const char *encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007238static void
7239xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7240 xmlNodePtr cur, int level, int format, const char *encoding);
7241
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007242void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
7243
Owen Taylor3473f882001-02-23 17:55:21 +00007244/**
7245 * xmlNsDumpOutput:
7246 * @buf: the XML buffer output
7247 * @cur: a namespace
7248 *
7249 * Dump a local Namespace definition.
7250 * Should be called in the context of attributes dumps.
7251 */
7252static void
7253xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7254 if (cur == NULL) {
7255#ifdef DEBUG_TREE
7256 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007257 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007258#endif
7259 return;
7260 }
7261 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00007262 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
7263 return;
7264
Owen Taylor3473f882001-02-23 17:55:21 +00007265 /* Within the context of an element attributes */
7266 if (cur->prefix != NULL) {
7267 xmlOutputBufferWriteString(buf, " xmlns:");
7268 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
7269 } else
7270 xmlOutputBufferWriteString(buf, " xmlns");
7271 xmlOutputBufferWriteString(buf, "=");
7272 xmlBufferWriteQuotedString(buf->buffer, cur->href);
7273 }
7274}
7275
7276/**
7277 * xmlNsListDumpOutput:
7278 * @buf: the XML buffer output
7279 * @cur: the first namespace
7280 *
7281 * Dump a list of local Namespace definitions.
7282 * Should be called in the context of attributes dumps.
7283 */
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007284void
Owen Taylor3473f882001-02-23 17:55:21 +00007285xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7286 while (cur != NULL) {
7287 xmlNsDumpOutput(buf, cur);
7288 cur = cur->next;
7289 }
7290}
7291
7292/**
7293 * xmlDtdDumpOutput:
7294 * @buf: the XML buffer output
7295 * @doc: the document
7296 * @encoding: an optional encoding string
7297 *
7298 * Dump the XML document DTD, if any.
7299 */
7300static void
7301xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
7302 if (dtd == NULL) {
7303#ifdef DEBUG_TREE
7304 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007305 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007306#endif
7307 return;
7308 }
7309 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
7310 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
7311 if (dtd->ExternalID != NULL) {
7312 xmlOutputBufferWriteString(buf, " PUBLIC ");
7313 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
7314 xmlOutputBufferWriteString(buf, " ");
7315 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7316 } else if (dtd->SystemID != NULL) {
7317 xmlOutputBufferWriteString(buf, " SYSTEM ");
7318 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7319 }
7320 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
7321 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
7322 xmlOutputBufferWriteString(buf, ">");
7323 return;
7324 }
7325 xmlOutputBufferWriteString(buf, " [\n");
7326 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
7327 xmlOutputBufferWriteString(buf, "]>");
7328}
7329
7330/**
7331 * xmlAttrDumpOutput:
7332 * @buf: the XML buffer output
7333 * @doc: the document
7334 * @cur: the attribute pointer
7335 * @encoding: an optional encoding string
7336 *
7337 * Dump an XML attribute
7338 */
7339static void
7340xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00007341 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00007342 if (cur == NULL) {
7343#ifdef DEBUG_TREE
7344 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007345 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007346#endif
7347 return;
7348 }
7349 xmlOutputBufferWriteString(buf, " ");
7350 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7351 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7352 xmlOutputBufferWriteString(buf, ":");
7353 }
7354 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillarda6d05382002-02-13 13:07:41 +00007355 xmlOutputBufferWriteString(buf, "=\"");
7356 xmlAttrSerializeContent(buf->buffer, doc, cur);
7357 xmlOutputBufferWriteString(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007358}
7359
7360/**
7361 * xmlAttrListDumpOutput:
7362 * @buf: the XML buffer output
7363 * @doc: the document
7364 * @cur: the first attribute pointer
7365 * @encoding: an optional encoding string
7366 *
7367 * Dump a list of XML attributes
7368 */
7369static void
7370xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7371 xmlAttrPtr cur, const char *encoding) {
7372 if (cur == NULL) {
7373#ifdef DEBUG_TREE
7374 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007375 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007376#endif
7377 return;
7378 }
7379 while (cur != NULL) {
7380 xmlAttrDumpOutput(buf, doc, cur, encoding);
7381 cur = cur->next;
7382 }
7383}
7384
7385
7386
7387/**
7388 * xmlNodeListDumpOutput:
7389 * @buf: the XML buffer output
7390 * @doc: the document
7391 * @cur: the first node
7392 * @level: the imbrication level for indenting
7393 * @format: is formatting allowed
7394 * @encoding: an optional encoding string
7395 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007396 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007397 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007398 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007399 */
7400static void
7401xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7402 xmlNodePtr cur, int level, int format, const char *encoding) {
7403 int i;
7404
7405 if (cur == NULL) {
7406#ifdef DEBUG_TREE
7407 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007408 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007409#endif
7410 return;
7411 }
7412 while (cur != NULL) {
7413 if ((format) && (xmlIndentTreeOutput) &&
7414 (cur->type == XML_ELEMENT_NODE))
7415 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007416 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007417 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007418 if (format) {
7419 xmlOutputBufferWriteString(buf, "\n");
7420 }
7421 cur = cur->next;
7422 }
7423}
7424
7425/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007426 * xmlNodeDumpOutputInternal:
Owen Taylor3473f882001-02-23 17:55:21 +00007427 * @buf: the XML buffer output
7428 * @doc: the document
7429 * @cur: the current node
7430 * @level: the imbrication level for indenting
7431 * @format: is formatting allowed
7432 * @encoding: an optional encoding string
7433 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007434 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007435 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007436 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007437 */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007438static void
7439xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7440 xmlNodePtr cur, int level, int format, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00007441 int i;
7442 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00007443 xmlChar *start, *end;
Owen Taylor3473f882001-02-23 17:55:21 +00007444
7445 if (cur == NULL) {
7446#ifdef DEBUG_TREE
7447 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007448 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007449#endif
7450 return;
7451 }
7452 if (cur->type == XML_XINCLUDE_START)
7453 return;
7454 if (cur->type == XML_XINCLUDE_END)
7455 return;
7456 if (cur->type == XML_DTD_NODE) {
7457 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
7458 return;
7459 }
Daniel Veillardcec50a62003-10-28 13:26:51 +00007460 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
7461 xmlNodeListDumpOutput(buf, doc, cur->children, level, format, encoding);
7462 return;
7463 }
Owen Taylor3473f882001-02-23 17:55:21 +00007464 if (cur->type == XML_ELEMENT_DECL) {
7465 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
7466 return;
7467 }
7468 if (cur->type == XML_ATTRIBUTE_DECL) {
7469 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
7470 return;
7471 }
7472 if (cur->type == XML_ENTITY_DECL) {
7473 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
7474 return;
7475 }
7476 if (cur->type == XML_TEXT_NODE) {
7477 if (cur->content != NULL) {
7478 if ((cur->name == xmlStringText) ||
7479 (cur->name != xmlStringTextNoenc)) {
7480 xmlChar *buffer;
7481
Owen Taylor3473f882001-02-23 17:55:21 +00007482 if (encoding == NULL)
7483 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7484 else
7485 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007486 if (buffer != NULL) {
7487 xmlOutputBufferWriteString(buf, (const char *)buffer);
7488 xmlFree(buffer);
7489 }
7490 } else {
7491 /*
7492 * Disable escaping, needed for XSLT
7493 */
Owen Taylor3473f882001-02-23 17:55:21 +00007494 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007495 }
7496 }
7497
7498 return;
7499 }
7500 if (cur->type == XML_PI_NODE) {
7501 if (cur->content != NULL) {
7502 xmlOutputBufferWriteString(buf, "<?");
7503 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7504 if (cur->content != NULL) {
7505 xmlOutputBufferWriteString(buf, " ");
Owen Taylor3473f882001-02-23 17:55:21 +00007506 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007507 }
7508 xmlOutputBufferWriteString(buf, "?>");
7509 } else {
7510 xmlOutputBufferWriteString(buf, "<?");
7511 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7512 xmlOutputBufferWriteString(buf, "?>");
7513 }
7514 return;
7515 }
7516 if (cur->type == XML_COMMENT_NODE) {
7517 if (cur->content != NULL) {
7518 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +00007519 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007520 xmlOutputBufferWriteString(buf, "-->");
7521 }
7522 return;
7523 }
7524 if (cur->type == XML_ENTITY_REF_NODE) {
7525 xmlOutputBufferWriteString(buf, "&");
7526 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7527 xmlOutputBufferWriteString(buf, ";");
7528 return;
7529 }
7530 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00007531 start = end = cur->content;
7532 while (*end != '\0') {
7533 if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) {
7534 end = end + 2;
7535 xmlOutputBufferWriteString(buf, "<![CDATA[");
7536 xmlOutputBufferWrite(buf, end - start, (const char *)start);
7537 xmlOutputBufferWriteString(buf, "]]>");
7538 start = end;
7539 }
7540 end++;
7541 }
7542 if (start != end) {
7543 xmlOutputBufferWriteString(buf, "<![CDATA[");
7544 xmlOutputBufferWriteString(buf, (const char *)start);
7545 xmlOutputBufferWriteString(buf, "]]>");
7546 }
Owen Taylor3473f882001-02-23 17:55:21 +00007547 return;
7548 }
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007549 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillarda1a9d042003-03-18 16:53:17 +00007550 xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding);
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007551 return;
7552 }
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007553 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007554 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007555 return;
7556 }
Owen Taylor3473f882001-02-23 17:55:21 +00007557
7558 if (format == 1) {
7559 tmp = cur->children;
7560 while (tmp != NULL) {
William M. Brack9ca682f2003-10-19 10:01:59 +00007561 if ((tmp->type == XML_TEXT_NODE) ||
7562 (tmp->type == XML_CDATA_SECTION_NODE) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007563 (tmp->type == XML_ENTITY_REF_NODE)) {
7564 format = 0;
7565 break;
7566 }
7567 tmp = tmp->next;
7568 }
7569 }
7570 xmlOutputBufferWriteString(buf, "<");
7571 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7572 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7573 xmlOutputBufferWriteString(buf, ":");
7574 }
7575
7576 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7577 if (cur->nsDef)
7578 xmlNsListDumpOutput(buf, cur->nsDef);
7579 if (cur->properties != NULL)
7580 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
7581
Daniel Veillard7db37732001-07-12 01:20:08 +00007582 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
7583 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007584 xmlOutputBufferWriteString(buf, "/>");
7585 return;
7586 }
7587 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00007588 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007589 xmlChar *buffer;
7590
Owen Taylor3473f882001-02-23 17:55:21 +00007591 if (encoding == NULL)
7592 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7593 else
7594 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007595 if (buffer != NULL) {
7596 xmlOutputBufferWriteString(buf, (const char *)buffer);
7597 xmlFree(buffer);
7598 }
7599 }
7600 if (cur->children != NULL) {
7601 if (format) xmlOutputBufferWriteString(buf, "\n");
7602 xmlNodeListDumpOutput(buf, doc, cur->children,
7603 (level >= 0?level+1:-1), format, encoding);
7604 if ((xmlIndentTreeOutput) && (format))
7605 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007606 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Owen Taylor3473f882001-02-23 17:55:21 +00007607 }
7608 xmlOutputBufferWriteString(buf, "</");
7609 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7610 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7611 xmlOutputBufferWriteString(buf, ":");
7612 }
7613
7614 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7615 xmlOutputBufferWriteString(buf, ">");
7616}
7617
7618/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007619 * xmlNodeDumpOutput:
7620 * @buf: the XML buffer output
7621 * @doc: the document
7622 * @cur: the current node
7623 * @level: the imbrication level for indenting
7624 * @format: is formatting allowed
7625 * @encoding: an optional encoding string
7626 *
7627 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007628 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007629 * or xmlKeepBlanksDefault(0) was called
7630 */
7631void
7632xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007633 int level, int format, const char *encoding)
7634{
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007635#ifdef LIBXML_HTML_ENABLED
7636 xmlDtdPtr dtd;
7637 int is_xhtml = 0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007638#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007639
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007640 xmlInitParser();
7641
7642#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007643 dtd = xmlGetIntSubset(doc);
7644 if (dtd != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007645 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7646 if (is_xhtml < 0)
7647 is_xhtml = 0;
7648 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
7649 (cur->type == XML_ELEMENT_NODE) &&
7650 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
7651 if (encoding != NULL)
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007652 htmlSetMetaEncoding((htmlDocPtr) doc,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007653 (const xmlChar *) encoding);
7654 else
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007655 htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007656 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007657 }
7658
7659 if (is_xhtml)
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007660 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007661 else
7662#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007663 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007664}
7665
7666/**
Owen Taylor3473f882001-02-23 17:55:21 +00007667 * xmlDocContentDumpOutput:
7668 * @buf: the XML buffer output
7669 * @cur: the document
7670 * @encoding: an optional encoding string
7671 * @format: should formatting spaces been added
7672 *
7673 * Dump an XML document.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007674 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007675 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007676 */
7677static void
7678xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
7679 const char *encoding, int format) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007680#ifdef LIBXML_HTML_ENABLED
7681 xmlDtdPtr dtd;
7682 int is_xhtml = 0;
7683#endif
7684
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007685 xmlInitParser();
7686
Owen Taylor3473f882001-02-23 17:55:21 +00007687 xmlOutputBufferWriteString(buf, "<?xml version=");
7688 if (cur->version != NULL)
7689 xmlBufferWriteQuotedString(buf->buffer, cur->version);
7690 else
7691 xmlOutputBufferWriteString(buf, "\"1.0\"");
7692 if (encoding == NULL) {
7693 if (cur->encoding != NULL)
7694 encoding = (const char *) cur->encoding;
7695 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
7696 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
7697 }
7698 if (encoding != NULL) {
7699 xmlOutputBufferWriteString(buf, " encoding=");
7700 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
7701 }
7702 switch (cur->standalone) {
7703 case 0:
7704 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
7705 break;
7706 case 1:
7707 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
7708 break;
7709 }
7710 xmlOutputBufferWriteString(buf, "?>\n");
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007711
7712#ifdef LIBXML_HTML_ENABLED
7713 dtd = xmlGetIntSubset(cur);
7714 if (dtd != NULL) {
7715 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7716 if (is_xhtml < 0) is_xhtml = 0;
7717 }
7718 if (is_xhtml) {
7719 if (encoding != NULL)
7720 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
7721 else
7722 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
7723 }
7724#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007725 if (cur->children != NULL) {
7726 xmlNodePtr child = cur->children;
7727
7728 while (child != NULL) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007729#ifdef LIBXML_HTML_ENABLED
7730 if (is_xhtml)
7731 xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
7732 else
7733#endif
7734 xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007735 xmlOutputBufferWriteString(buf, "\n");
7736 child = child->next;
7737 }
7738 }
7739}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007740#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007741
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007742#ifdef LIBXML_HTML_ENABLED
7743/************************************************************************
7744 * *
7745 * Functions specific to XHTML serialization *
7746 * *
7747 ************************************************************************/
7748
7749#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
7750 "-//W3C//DTD XHTML 1.0 Strict//EN"
7751#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
7752 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
7753#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
7754 "-//W3C//DTD XHTML 1.0 Frameset//EN"
7755#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
7756 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
7757#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
7758 "-//W3C//DTD XHTML 1.0 Transitional//EN"
7759#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
7760 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
7761
7762#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
7763/**
7764 * xmlIsXHTML:
7765 * @systemID: the system identifier
7766 * @publicID: the public identifier
7767 *
7768 * Try to find if the document correspond to an XHTML DTD
7769 *
7770 * Returns 1 if true, 0 if not and -1 in case of error
7771 */
7772int
7773xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
7774 if ((systemID == NULL) && (publicID == NULL))
7775 return(-1);
7776 if (publicID != NULL) {
7777 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
7778 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
7779 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
7780 }
7781 if (systemID != NULL) {
7782 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
7783 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
7784 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
7785 }
7786 return(0);
7787}
7788
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007789#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007790/**
7791 * xhtmlIsEmpty:
7792 * @node: the node
7793 *
7794 * Check if a node is an empty xhtml node
7795 *
7796 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
7797 */
7798static int
7799xhtmlIsEmpty(xmlNodePtr node) {
7800 if (node == NULL)
7801 return(-1);
7802 if (node->type != XML_ELEMENT_NODE)
7803 return(0);
7804 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
7805 return(0);
7806 if (node->children != NULL)
7807 return(0);
7808 switch (node->name[0]) {
7809 case 'a':
7810 if (xmlStrEqual(node->name, BAD_CAST "area"))
7811 return(1);
7812 return(0);
7813 case 'b':
7814 if (xmlStrEqual(node->name, BAD_CAST "br"))
7815 return(1);
7816 if (xmlStrEqual(node->name, BAD_CAST "base"))
7817 return(1);
7818 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
7819 return(1);
7820 return(0);
7821 case 'c':
7822 if (xmlStrEqual(node->name, BAD_CAST "col"))
7823 return(1);
7824 return(0);
7825 case 'f':
7826 if (xmlStrEqual(node->name, BAD_CAST "frame"))
7827 return(1);
7828 return(0);
7829 case 'h':
7830 if (xmlStrEqual(node->name, BAD_CAST "hr"))
7831 return(1);
7832 return(0);
7833 case 'i':
7834 if (xmlStrEqual(node->name, BAD_CAST "img"))
7835 return(1);
7836 if (xmlStrEqual(node->name, BAD_CAST "input"))
7837 return(1);
7838 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
7839 return(1);
7840 return(0);
7841 case 'l':
7842 if (xmlStrEqual(node->name, BAD_CAST "link"))
7843 return(1);
7844 return(0);
7845 case 'm':
7846 if (xmlStrEqual(node->name, BAD_CAST "meta"))
7847 return(1);
7848 return(0);
7849 case 'p':
7850 if (xmlStrEqual(node->name, BAD_CAST "param"))
7851 return(1);
7852 return(0);
7853 }
7854 return(0);
7855}
7856
7857/**
7858 * xhtmlAttrListDumpOutput:
7859 * @buf: the XML buffer output
7860 * @doc: the document
7861 * @cur: the first attribute pointer
7862 * @encoding: an optional encoding string
7863 *
7864 * Dump a list of XML attributes
7865 */
7866static void
7867xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7868 xmlAttrPtr cur, const char *encoding) {
7869 xmlAttrPtr xml_lang = NULL;
7870 xmlAttrPtr lang = NULL;
7871 xmlAttrPtr name = NULL;
7872 xmlAttrPtr id = NULL;
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007873 xmlNodePtr parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007874
7875 if (cur == NULL) {
7876#ifdef DEBUG_TREE
7877 xmlGenericError(xmlGenericErrorContext,
7878 "xmlAttrListDumpOutput : property == NULL\n");
7879#endif
7880 return;
7881 }
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007882 parent = cur->parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007883 while (cur != NULL) {
7884 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
7885 id = cur;
7886 else
7887 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
7888 name = cur;
7889 else
7890 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
7891 lang = cur;
7892 else
7893 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
7894 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
7895 xml_lang = cur;
7896 else if ((cur->ns == NULL) &&
7897 ((cur->children == NULL) ||
7898 (cur->children->content == NULL) ||
7899 (cur->children->content[0] == 0)) &&
7900 (htmlIsBooleanAttr(cur->name))) {
7901 if (cur->children != NULL)
7902 xmlFreeNode(cur->children);
7903 cur->children = xmlNewText(cur->name);
7904 if (cur->children != NULL)
7905 cur->children->parent = (xmlNodePtr) cur;
7906 }
7907 xmlAttrDumpOutput(buf, doc, cur, encoding);
7908 cur = cur->next;
7909 }
7910 /*
7911 * C.8
7912 */
7913 if ((name != NULL) && (id == NULL)) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007914 if ((parent != NULL) && (parent->name != NULL) &&
7915 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
7916 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
7917 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
7918 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
7919 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
7920 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
7921 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
7922 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
7923 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
7924 xmlOutputBufferWriteString(buf, " id=\"");
7925 xmlAttrSerializeContent(buf->buffer, doc, name);
7926 xmlOutputBufferWriteString(buf, "\"");
7927 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007928 }
7929 /*
7930 * C.7.
7931 */
7932 if ((lang != NULL) && (xml_lang == NULL)) {
7933 xmlOutputBufferWriteString(buf, " xml:lang=\"");
7934 xmlAttrSerializeContent(buf->buffer, doc, lang);
7935 xmlOutputBufferWriteString(buf, "\"");
7936 } else
7937 if ((xml_lang != NULL) && (lang == NULL)) {
7938 xmlOutputBufferWriteString(buf, " lang=\"");
7939 xmlAttrSerializeContent(buf->buffer, doc, xml_lang);
7940 xmlOutputBufferWriteString(buf, "\"");
7941 }
7942}
7943
7944/**
7945 * xhtmlNodeListDumpOutput:
7946 * @buf: the XML buffer output
7947 * @doc: the XHTML document
7948 * @cur: the first node
7949 * @level: the imbrication level for indenting
7950 * @format: is formatting allowed
7951 * @encoding: an optional encoding string
7952 *
7953 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007954 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007955 * or xmlKeepBlanksDefault(0) was called
7956 */
7957static void
7958xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7959 xmlNodePtr cur, int level, int format, const char *encoding) {
7960 int i;
7961
7962 if (cur == NULL) {
7963#ifdef DEBUG_TREE
7964 xmlGenericError(xmlGenericErrorContext,
7965 "xhtmlNodeListDumpOutput : node == NULL\n");
7966#endif
7967 return;
7968 }
7969 while (cur != NULL) {
7970 if ((format) && (xmlIndentTreeOutput) &&
7971 (cur->type == XML_ELEMENT_NODE))
7972 for (i = 0;i < level;i++)
7973 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
7974 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
7975 if (format) {
7976 xmlOutputBufferWriteString(buf, "\n");
7977 }
7978 cur = cur->next;
7979 }
7980}
7981
7982/**
7983 * xhtmlNodeDumpOutput:
7984 * @buf: the XML buffer output
7985 * @doc: the XHTML document
7986 * @cur: the current node
7987 * @level: the imbrication level for indenting
7988 * @format: is formatting allowed
7989 * @encoding: an optional encoding string
7990 *
7991 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007992 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007993 * or xmlKeepBlanksDefault(0) was called
7994 */
7995static void
7996xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7997 int level, int format, const char *encoding) {
7998 int i;
7999 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00008000 xmlChar *start, *end;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008001
8002 if (cur == NULL) {
8003#ifdef DEBUG_TREE
8004 xmlGenericError(xmlGenericErrorContext,
8005 "xmlNodeDumpOutput : node == NULL\n");
8006#endif
8007 return;
8008 }
8009 if (cur->type == XML_XINCLUDE_START)
8010 return;
8011 if (cur->type == XML_XINCLUDE_END)
8012 return;
8013 if (cur->type == XML_DTD_NODE) {
8014 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
8015 return;
8016 }
8017 if (cur->type == XML_ELEMENT_DECL) {
8018 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
8019 return;
8020 }
8021 if (cur->type == XML_ATTRIBUTE_DECL) {
8022 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
8023 return;
8024 }
8025 if (cur->type == XML_ENTITY_DECL) {
8026 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
8027 return;
8028 }
8029 if (cur->type == XML_TEXT_NODE) {
8030 if (cur->content != NULL) {
8031 if ((cur->name == xmlStringText) ||
8032 (cur->name != xmlStringTextNoenc)) {
8033 xmlChar *buffer;
8034
8035 if (encoding == NULL)
8036 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
8037 else
8038 buffer = xmlEncodeSpecialChars(doc, cur->content);
8039 if (buffer != NULL) {
8040 xmlOutputBufferWriteString(buf, (const char *)buffer);
8041 xmlFree(buffer);
8042 }
8043 } else {
8044 /*
8045 * Disable escaping, needed for XSLT
8046 */
8047 xmlOutputBufferWriteString(buf, (const char *) cur->content);
8048 }
8049 }
8050
8051 return;
8052 }
8053 if (cur->type == XML_PI_NODE) {
8054 if (cur->content != NULL) {
8055 xmlOutputBufferWriteString(buf, "<?");
8056 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8057 if (cur->content != NULL) {
8058 xmlOutputBufferWriteString(buf, " ");
8059 xmlOutputBufferWriteString(buf, (const char *)cur->content);
8060 }
8061 xmlOutputBufferWriteString(buf, "?>");
8062 } else {
8063 xmlOutputBufferWriteString(buf, "<?");
8064 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8065 xmlOutputBufferWriteString(buf, "?>");
8066 }
8067 return;
8068 }
8069 if (cur->type == XML_COMMENT_NODE) {
8070 if (cur->content != NULL) {
8071 xmlOutputBufferWriteString(buf, "<!--");
8072 xmlOutputBufferWriteString(buf, (const char *)cur->content);
8073 xmlOutputBufferWriteString(buf, "-->");
8074 }
8075 return;
8076 }
8077 if (cur->type == XML_ENTITY_REF_NODE) {
8078 xmlOutputBufferWriteString(buf, "&");
8079 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8080 xmlOutputBufferWriteString(buf, ";");
8081 return;
8082 }
8083 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00008084 start = end = cur->content;
8085 while (*end != '\0') {
8086 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
8087 end = end + 2;
8088 xmlOutputBufferWriteString(buf, "<![CDATA[");
8089 xmlOutputBufferWrite(buf, end - start, (const char *)start);
8090 xmlOutputBufferWriteString(buf, "]]>");
8091 start = end;
8092 }
8093 end++;
8094 }
8095 if (start != end) {
8096 xmlOutputBufferWriteString(buf, "<![CDATA[");
8097 xmlOutputBufferWriteString(buf, (const char *)start);
8098 xmlOutputBufferWriteString(buf, "]]>");
8099 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008100 return;
8101 }
8102
8103 if (format == 1) {
8104 tmp = cur->children;
8105 while (tmp != NULL) {
8106 if ((tmp->type == XML_TEXT_NODE) ||
8107 (tmp->type == XML_ENTITY_REF_NODE)) {
8108 format = 0;
8109 break;
8110 }
8111 tmp = tmp->next;
8112 }
8113 }
8114 xmlOutputBufferWriteString(buf, "<");
8115 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8116 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8117 xmlOutputBufferWriteString(buf, ":");
8118 }
8119
8120 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8121 if (cur->nsDef)
8122 xmlNsListDumpOutput(buf, cur->nsDef);
8123 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
8124 (cur->ns == NULL) && (cur->nsDef == NULL))) {
8125 /*
8126 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
8127 */
8128 xmlOutputBufferWriteString(buf,
8129 " xmlns=\"http://www.w3.org/1999/xhtml\"");
8130 }
8131 if (cur->properties != NULL)
8132 xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
8133
8134 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
8135 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
8136 (xhtmlIsEmpty(cur) == 1)) {
8137 /*
8138 * C.2. Empty Elements
8139 */
8140 xmlOutputBufferWriteString(buf, " />");
8141 } else {
8142 /*
8143 * C.3. Element Minimization and Empty Element Content
8144 */
8145 xmlOutputBufferWriteString(buf, "></");
8146 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8147 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8148 xmlOutputBufferWriteString(buf, ":");
8149 }
8150 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8151 xmlOutputBufferWriteString(buf, ">");
8152 }
8153 return;
8154 }
8155 xmlOutputBufferWriteString(buf, ">");
8156 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
8157 xmlChar *buffer;
8158
8159 if (encoding == NULL)
8160 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
8161 else
8162 buffer = xmlEncodeSpecialChars(doc, cur->content);
8163 if (buffer != NULL) {
8164 xmlOutputBufferWriteString(buf, (const char *)buffer);
8165 xmlFree(buffer);
8166 }
8167 }
8168
8169 /*
8170 * 4.8. Script and Style elements
8171 */
8172 if ((cur->type == XML_ELEMENT_NODE) &&
8173 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
8174 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
8175 ((cur->ns == NULL) ||
8176 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
8177 xmlNodePtr child = cur->children;
8178
8179 while (child != NULL) {
8180 if ((child->type == XML_TEXT_NODE) ||
8181 (child->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard64b35282002-12-04 15:10:40 +00008182 /*
8183 * Apparently CDATA escaping for style just break on IE,
8184 * mozilla and galeon, so ...
8185 */
8186 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
8187 (xmlStrchr(child->content, '<') == NULL) &&
8188 (xmlStrchr(child->content, '>') == NULL) &&
8189 (xmlStrchr(child->content, '&') == NULL)) {
8190 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8191 } else {
Daniel Veillard9475a352003-09-26 12:47:50 +00008192 start = end = child->content;
8193 while (*end != '\0') {
8194 if (*end == ']' &&
8195 *(end + 1) == ']' &&
8196 *(end + 2) == '>') {
8197 end = end + 2;
8198 xmlOutputBufferWriteString(buf, "<![CDATA[");
8199 xmlOutputBufferWrite(buf, end - start,
8200 (const char *)start);
8201 xmlOutputBufferWriteString(buf, "]]>");
8202 start = end;
8203 }
8204 end++;
8205 }
8206 if (start != end) {
8207 xmlOutputBufferWriteString(buf, "<![CDATA[");
8208 xmlOutputBufferWriteString(buf, (const char *)start);
8209 xmlOutputBufferWriteString(buf, "]]>");
8210 }
Daniel Veillard64b35282002-12-04 15:10:40 +00008211 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008212 } else {
8213 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8214 }
8215 child = child->next;
8216 }
8217 } else if (cur->children != NULL) {
8218 if (format) xmlOutputBufferWriteString(buf, "\n");
8219 xhtmlNodeListDumpOutput(buf, doc, cur->children,
8220 (level >= 0?level+1:-1), format, encoding);
8221 if ((xmlIndentTreeOutput) && (format))
8222 for (i = 0;i < level;i++)
8223 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
8224 }
8225 xmlOutputBufferWriteString(buf, "</");
8226 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8227 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8228 xmlOutputBufferWriteString(buf, ":");
8229 }
8230
8231 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8232 xmlOutputBufferWriteString(buf, ">");
8233}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008234#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008235#endif
8236
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008237#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008238/************************************************************************
8239 * *
8240 * Saving functions front-ends *
8241 * *
8242 ************************************************************************/
8243
8244/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00008245 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00008246 * @out_doc: Document to generate XML text from
8247 * @doc_txt_ptr: Memory pointer for allocated XML text
8248 * @doc_txt_len: Length of the generated XML text
8249 * @txt_encoding: Character encoding to use when generating XML text
8250 * @format: should formatting spaces been added
8251 *
8252 * Dump the current DOM tree into memory using the character encoding specified
8253 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008254 * allocated memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008255 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008256 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008257 */
8258
8259void
8260xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008261 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008262 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008263 int dummy = 0;
8264
Owen Taylor3473f882001-02-23 17:55:21 +00008265 xmlOutputBufferPtr out_buff = NULL;
8266 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
8267
8268 if (doc_txt_len == NULL) {
8269 doc_txt_len = &dummy; /* Continue, caller just won't get length */
8270 }
8271
8272 if (doc_txt_ptr == NULL) {
8273 *doc_txt_len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008274 return;
8275 }
8276
8277 *doc_txt_ptr = NULL;
8278 *doc_txt_len = 0;
8279
8280 if (out_doc == NULL) {
8281 /* No document, no output */
Owen Taylor3473f882001-02-23 17:55:21 +00008282 return;
8283 }
8284
8285 /*
8286 * Validate the encoding value, if provided.
8287 * This logic is copied from xmlSaveFileEnc.
8288 */
8289
8290 if (txt_encoding == NULL)
8291 txt_encoding = (const char *) out_doc->encoding;
8292 if (txt_encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008293 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008294 if ( conv_hdlr == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008295 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
8296 txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008297 return;
8298 }
8299 }
Owen Taylor3473f882001-02-23 17:55:21 +00008300
8301 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008302 xmlSaveErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00008303 return;
8304 }
8305
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008306 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008307 xmlOutputBufferFlush(out_buff);
8308 if (out_buff->conv != NULL) {
8309 *doc_txt_len = out_buff->conv->use;
8310 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
8311 } else {
8312 *doc_txt_len = out_buff->buffer->use;
8313 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
8314 }
8315 (void)xmlOutputBufferClose(out_buff);
8316
8317 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
8318 *doc_txt_len = 0;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008319 xmlSaveErrMemory("creating output");
Owen Taylor3473f882001-02-23 17:55:21 +00008320 }
8321
8322 return;
8323}
8324
8325/**
8326 * xmlDocDumpMemory:
8327 * @cur: the document
8328 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008329 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008330 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008331 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008332 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008333 */
8334void
8335xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
8336 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
8337}
8338
8339/**
8340 * xmlDocDumpFormatMemory:
8341 * @cur: the document
8342 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008343 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008344 * @format: should formatting spaces been added
8345 *
8346 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008347 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008348 * It's up to the caller to free the memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008349 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008350 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008351 */
8352void
8353xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
8354 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
8355}
8356
8357/**
8358 * xmlDocDumpMemoryEnc:
8359 * @out_doc: Document to generate XML text from
8360 * @doc_txt_ptr: Memory pointer for allocated XML text
8361 * @doc_txt_len: Length of the generated XML text
8362 * @txt_encoding: Character encoding to use when generating XML text
8363 *
8364 * Dump the current DOM tree into memory using the character encoding specified
8365 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008366 * allocated memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008367 */
8368
8369void
8370xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
8371 int * doc_txt_len, const char * txt_encoding) {
8372 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008373 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008374}
8375
8376/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008377 * xmlDocFormatDump:
Owen Taylor3473f882001-02-23 17:55:21 +00008378 * @f: the FILE*
8379 * @cur: the document
Daniel Veillard9e412302002-06-10 15:59:44 +00008380 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008381 *
8382 * Dump an XML document to an open FILE.
8383 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008384 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008385 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8386 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008387 */
8388int
Daniel Veillard9e412302002-06-10 15:59:44 +00008389xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008390 xmlOutputBufferPtr buf;
8391 const char * encoding;
8392 xmlCharEncodingHandlerPtr handler = NULL;
8393 int ret;
8394
8395 if (cur == NULL) {
8396#ifdef DEBUG_TREE
8397 xmlGenericError(xmlGenericErrorContext,
8398 "xmlDocDump : document == NULL\n");
8399#endif
8400 return(-1);
8401 }
8402 encoding = (const char *) cur->encoding;
8403
8404 if (encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008405 handler = xmlFindCharEncodingHandler(encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008406 if (handler == NULL) {
8407 xmlFree((char *) cur->encoding);
8408 cur->encoding = NULL;
8409 }
8410 }
Owen Taylor3473f882001-02-23 17:55:21 +00008411 buf = xmlOutputBufferCreateFile(f, handler);
8412 if (buf == NULL) return(-1);
Daniel Veillard9e412302002-06-10 15:59:44 +00008413 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008414
8415 ret = xmlOutputBufferClose(buf);
8416 return(ret);
8417}
8418
8419/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008420 * xmlDocDump:
8421 * @f: the FILE*
8422 * @cur: the document
8423 *
8424 * Dump an XML document to an open FILE.
8425 *
8426 * returns: the number of bytes written or -1 in case of failure.
8427 */
8428int
8429xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard828ce832003-10-08 19:19:10 +00008430 return(xmlDocFormatDump (f, cur, 0));
Daniel Veillard9e412302002-06-10 15:59:44 +00008431}
8432
8433/**
Owen Taylor3473f882001-02-23 17:55:21 +00008434 * xmlSaveFileTo:
8435 * @buf: an output I/O buffer
8436 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008437 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00008438 *
8439 * Dump an XML document to an I/O buffer.
8440 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008441 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008442 */
8443int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008444xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00008445 int ret;
8446
8447 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008448 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008449 ret = xmlOutputBufferClose(buf);
8450 return(ret);
8451}
8452
8453/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00008454 * xmlSaveFormatFileTo:
8455 * @buf: an output I/O buffer
8456 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008457 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00008458 * @format: should formatting spaces been added
8459 *
8460 * Dump an XML document to an I/O buffer.
8461 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008462 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008463 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8464 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardeefd4492001-04-28 16:55:50 +00008465 */
8466int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008467xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00008468 int ret;
8469
8470 if (buf == NULL) return(0);
8471 xmlDocContentDumpOutput(buf, cur, encoding, format);
8472 ret = xmlOutputBufferClose(buf);
8473 return(ret);
8474}
8475
8476/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00008477 * xmlSaveFormatFileEnc:
Daniel Veillardf012a642001-07-23 19:10:52 +00008478 * @filename: the filename or URL to output
8479 * @cur: the document being saved
8480 * @encoding: the name of the encoding to use or NULL.
8481 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00008482 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00008483 * Dump an XML document to a file or an URL.
8484 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008485 * Returns the number of bytes written or -1 in case of error.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008486 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8487 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008488 */
8489int
Daniel Veillardf012a642001-07-23 19:10:52 +00008490xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
8491 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00008492 xmlOutputBufferPtr buf;
8493 xmlCharEncodingHandlerPtr handler = NULL;
8494 int ret;
8495
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00008496 if (cur == NULL)
8497 return(-1);
8498
Daniel Veillardfb25a512002-01-13 20:32:08 +00008499 if (encoding == NULL)
8500 encoding = (const char *) cur->encoding;
8501
Owen Taylor3473f882001-02-23 17:55:21 +00008502 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008503
Owen Taylor3473f882001-02-23 17:55:21 +00008504 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00008505 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00008506 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00008507 }
8508
Daniel Veillardf012a642001-07-23 19:10:52 +00008509#ifdef HAVE_ZLIB_H
8510 if (cur->compression < 0) cur->compression = xmlCompressMode;
8511#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008512 /*
8513 * save the content to a temp buffer.
8514 */
Daniel Veillardf012a642001-07-23 19:10:52 +00008515 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00008516 if (buf == NULL) return(-1);
8517
Daniel Veillardf012a642001-07-23 19:10:52 +00008518 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008519
8520 ret = xmlOutputBufferClose(buf);
8521 return(ret);
8522}
8523
Daniel Veillardf012a642001-07-23 19:10:52 +00008524
8525/**
8526 * xmlSaveFileEnc:
8527 * @filename: the filename (or URL)
8528 * @cur: the document
8529 * @encoding: the name of an encoding (or NULL)
8530 *
8531 * Dump an XML document, converting it to the given encoding
8532 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008533 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00008534 */
8535int
8536xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
8537 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
8538}
8539
Owen Taylor3473f882001-02-23 17:55:21 +00008540/**
Daniel Veillard67fee942001-04-26 18:59:03 +00008541 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00008542 * @filename: the filename (or URL)
8543 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00008544 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008545 *
8546 * Dump an XML document to a file. Will use compression if
8547 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00008548 * used. If @format is set then the document will be indented on output.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008549 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8550 * or xmlKeepBlanksDefault(0) was called
Daniel Veillard67fee942001-04-26 18:59:03 +00008551 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008552 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008553 */
8554int
Daniel Veillard67fee942001-04-26 18:59:03 +00008555xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008556 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00008557}
8558
Daniel Veillard67fee942001-04-26 18:59:03 +00008559/**
8560 * xmlSaveFile:
8561 * @filename: the filename (or URL)
8562 * @cur: the document
8563 *
8564 * Dump an XML document to a file. Will use compression if
8565 * compiled in and enabled. If @filename is "-" the stdout file is
8566 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00008567 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00008568 */
8569int
8570xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008571 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00008572}
8573
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008574#endif /* LIBXML_OUTPUT_ENABLED */
8575
8576/**
8577 * xmlGetDocCompressMode:
8578 * @doc: the document
8579 *
8580 * get the compression ratio for a document, ZLIB based
8581 * Returns 0 (uncompressed) to 9 (max compression)
8582 */
8583int
8584xmlGetDocCompressMode (xmlDocPtr doc) {
8585 if (doc == NULL) return(-1);
8586 return(doc->compression);
8587}
8588
8589/**
8590 * xmlSetDocCompressMode:
8591 * @doc: the document
8592 * @mode: the compression ratio
8593 *
8594 * set the compression ratio for a document, ZLIB based
8595 * Correct values: 0 (uncompressed) to 9 (max compression)
8596 */
8597void
8598xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
8599 if (doc == NULL) return;
8600 if (mode < 0) doc->compression = 0;
8601 else if (mode > 9) doc->compression = 9;
8602 else doc->compression = mode;
8603}
8604
8605/**
8606 * xmlGetCompressMode:
8607 *
8608 * get the default compression mode used, ZLIB based.
8609 * Returns 0 (uncompressed) to 9 (max compression)
8610 */
8611int
8612xmlGetCompressMode(void)
8613{
8614 return (xmlCompressMode);
8615}
8616
8617/**
8618 * xmlSetCompressMode:
8619 * @mode: the compression ratio
8620 *
8621 * set the default compression mode used, ZLIB based
8622 * Correct values: 0 (uncompressed) to 9 (max compression)
8623 */
8624void
8625xmlSetCompressMode(int mode) {
8626 if (mode < 0) xmlCompressMode = 0;
8627 else if (mode > 9) xmlCompressMode = 9;
8628 else xmlCompressMode = mode;
8629}
8630