blob: 0d984e2debf052d744b38b6c538f32a50de25a3d [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
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00002992 if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE) &&
2993 (cur->name == elem->name)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002994 xmlNodeAddContent(cur, elem->content);
Owen Taylor3473f882001-02-23 17:55:21 +00002995 xmlFreeNode(elem);
2996 return(cur);
2997 }
2998
2999 if (elem->doc != cur->doc) {
3000 xmlSetTreeDoc(elem, cur->doc);
3001 }
3002 parent = cur->parent;
3003 elem->prev = cur;
3004 elem->next = NULL;
3005 elem->parent = parent;
3006 cur->next = elem;
3007 if (parent != NULL)
3008 parent->last = elem;
3009
3010 return(elem);
3011}
3012
3013/**
3014 * xmlAddChildList:
3015 * @parent: the parent node
3016 * @cur: the first node in the list
3017 *
3018 * Add a list of node at the end of the child list of the parent
3019 * merging adjacent TEXT nodes (@cur may be freed)
3020 *
3021 * Returns the last child or NULL in case of error.
3022 */
3023xmlNodePtr
3024xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
3025 xmlNodePtr prev;
3026
3027 if (parent == NULL) {
3028#ifdef DEBUG_TREE
3029 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003030 "xmlAddChildList : parent == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003031#endif
3032 return(NULL);
3033 }
3034
3035 if (cur == NULL) {
3036#ifdef DEBUG_TREE
3037 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00003038 "xmlAddChildList : child == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003039#endif
3040 return(NULL);
3041 }
3042
3043 if ((cur->doc != NULL) && (parent->doc != NULL) &&
3044 (cur->doc != parent->doc)) {
3045#ifdef DEBUG_TREE
3046 xmlGenericError(xmlGenericErrorContext,
3047 "Elements moved to a different document\n");
3048#endif
3049 }
3050
3051 /*
3052 * add the first element at the end of the children list.
3053 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003054
Owen Taylor3473f882001-02-23 17:55:21 +00003055 if (parent->children == NULL) {
3056 parent->children = cur;
3057 } else {
3058 /*
3059 * If cur and parent->last both are TEXT nodes, then merge them.
3060 */
3061 if ((cur->type == XML_TEXT_NODE) &&
3062 (parent->last->type == XML_TEXT_NODE) &&
3063 (cur->name == parent->last->name)) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003064 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003065 /*
3066 * if it's the only child, nothing more to be done.
3067 */
3068 if (cur->next == NULL) {
3069 xmlFreeNode(cur);
3070 return(parent->last);
3071 }
3072 prev = cur;
3073 cur = cur->next;
3074 xmlFreeNode(prev);
3075 }
3076 prev = parent->last;
3077 prev->next = cur;
3078 cur->prev = prev;
3079 }
3080 while (cur->next != NULL) {
3081 cur->parent = parent;
3082 if (cur->doc != parent->doc) {
3083 xmlSetTreeDoc(cur, parent->doc);
3084 }
3085 cur = cur->next;
3086 }
3087 cur->parent = parent;
3088 cur->doc = parent->doc; /* the parent may not be linked to a doc ! */
3089 parent->last = cur;
3090
3091 return(cur);
3092}
3093
3094/**
3095 * xmlAddChild:
3096 * @parent: the parent node
3097 * @cur: the child node
3098 *
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003099 * Add a new node to @parent, at the end of the child (or property) list
Owen Taylor3473f882001-02-23 17:55:21 +00003100 * merging adjacent TEXT nodes (in which case @cur is freed)
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003101 * If the new node is ATTRIBUTE, it is added into properties instead of children.
3102 * If there is an attribute with equal name, it is first destroyed.
3103 *
Owen Taylor3473f882001-02-23 17:55:21 +00003104 * Returns the child or NULL in case of error.
3105 */
3106xmlNodePtr
3107xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
3108 xmlNodePtr prev;
3109
3110 if (parent == NULL) {
3111#ifdef DEBUG_TREE
3112 xmlGenericError(xmlGenericErrorContext,
3113 "xmlAddChild : parent == NULL\n");
3114#endif
3115 return(NULL);
3116 }
3117
3118 if (cur == NULL) {
3119#ifdef DEBUG_TREE
3120 xmlGenericError(xmlGenericErrorContext,
3121 "xmlAddChild : child == NULL\n");
3122#endif
3123 return(NULL);
3124 }
3125
Owen Taylor3473f882001-02-23 17:55:21 +00003126 /*
3127 * If cur is a TEXT node, merge its content with adjacent TEXT nodes
Owen Taylor3473f882001-02-23 17:55:21 +00003128 * cur is then freed.
3129 */
3130 if (cur->type == XML_TEXT_NODE) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003131 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003132 (parent->content != NULL) &&
Daniel Veillarde22dd5c2003-10-29 12:53:27 +00003133 (parent->name == cur->name) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003134 (parent != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003135 xmlNodeAddContent(parent, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003136 xmlFreeNode(cur);
3137 return(parent);
3138 }
3139 if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003140 (parent->last->name == cur->name) &&
3141 (parent->last != cur)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003142 xmlNodeAddContent(parent->last, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00003143 xmlFreeNode(cur);
3144 return(parent->last);
3145 }
3146 }
3147
3148 /*
3149 * add the new element at the end of the children list.
3150 */
Daniel Veillard5335dc52003-01-01 20:59:38 +00003151 prev = cur->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00003152 cur->parent = parent;
3153 if (cur->doc != parent->doc) {
3154 xmlSetTreeDoc(cur, parent->doc);
3155 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003156 /* this check prevents a loop on tree-traversions if a developer
3157 * tries to add a node to its parent multiple times
3158 */
3159 if (prev == parent)
3160 return(cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003161
3162 /*
Daniel Veillard7db37732001-07-12 01:20:08 +00003163 * Coalescing
Owen Taylor3473f882001-02-23 17:55:21 +00003164 */
Daniel Veillard7db37732001-07-12 01:20:08 +00003165 if ((parent->type == XML_TEXT_NODE) &&
Daniel Veillard5335dc52003-01-01 20:59:38 +00003166 (parent->content != NULL) &&
3167 (parent != cur)) {
Daniel Veillard7db37732001-07-12 01:20:08 +00003168 xmlNodeAddContent(parent, cur->content);
Daniel Veillard7db37732001-07-12 01:20:08 +00003169 xmlFreeNode(cur);
3170 return(parent);
Owen Taylor3473f882001-02-23 17:55:21 +00003171 }
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003172 if (cur->type == XML_ATTRIBUTE_NODE) {
3173 if (parent->properties == NULL) {
3174 parent->properties = (xmlAttrPtr) cur;
3175 } else {
3176 /* check if an attribute with the same name exists */
3177 xmlAttrPtr lastattr;
Owen Taylor3473f882001-02-23 17:55:21 +00003178
Daniel Veillardbd227ae2002-01-24 16:05:41 +00003179 if (cur->ns == NULL)
3180 lastattr = xmlHasProp(parent, cur->name);
3181 else
3182 lastattr = xmlHasNsProp(parent, cur->name, cur->ns->href);
3183 if ((lastattr != NULL) && (lastattr != (xmlAttrPtr) cur)) {
3184 /* different instance, destroy it (attributes must be unique) */
3185 xmlFreeProp(lastattr);
3186 }
3187 /* find the end */
3188 lastattr = parent->properties;
3189 while (lastattr->next != NULL) {
3190 lastattr = lastattr->next;
3191 }
3192 lastattr->next = (xmlAttrPtr) cur;
3193 ((xmlAttrPtr) cur)->prev = lastattr;
3194 }
3195 } else {
3196 if (parent->children == NULL) {
3197 parent->children = cur;
3198 parent->last = cur;
3199 } else {
3200 prev = parent->last;
3201 prev->next = cur;
3202 cur->prev = prev;
3203 parent->last = cur;
3204 }
3205 }
Owen Taylor3473f882001-02-23 17:55:21 +00003206 return(cur);
3207}
3208
3209/**
3210 * xmlGetLastChild:
3211 * @parent: the parent node
3212 *
3213 * Search the last child of a node.
3214 * Returns the last child or NULL if none.
3215 */
3216xmlNodePtr
3217xmlGetLastChild(xmlNodePtr parent) {
3218 if (parent == NULL) {
3219#ifdef DEBUG_TREE
3220 xmlGenericError(xmlGenericErrorContext,
3221 "xmlGetLastChild : parent == NULL\n");
3222#endif
3223 return(NULL);
3224 }
3225 return(parent->last);
3226}
3227
3228/**
3229 * xmlFreeNodeList:
3230 * @cur: the first node in the list
3231 *
3232 * Free a node and all its siblings, this is a recursive behaviour, all
3233 * the children are freed too.
3234 */
3235void
3236xmlFreeNodeList(xmlNodePtr cur) {
3237 xmlNodePtr next;
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003238 xmlDictPtr dict = NULL;
3239
3240 if (cur == NULL) return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003241 if (cur->type == XML_NAMESPACE_DECL) {
3242 xmlFreeNsList((xmlNsPtr) cur);
3243 return;
3244 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003245 if ((cur->type == XML_DOCUMENT_NODE) ||
3246#ifdef LIBXML_DOCB_ENABLED
3247 (cur->type == XML_DOCB_DOCUMENT_NODE) ||
Daniel Veillard9adc0462003-03-24 18:39:54 +00003248#endif
Daniel Veillard6560a422003-03-27 21:25:38 +00003249 (cur->type == XML_HTML_DOCUMENT_NODE)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003250 xmlFreeDoc((xmlDocPtr) cur);
3251 return;
3252 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003253 if (cur->doc != NULL) dict = cur->doc->dict;
Owen Taylor3473f882001-02-23 17:55:21 +00003254 while (cur != NULL) {
3255 next = cur->next;
Daniel Veillard02141ea2001-04-30 11:46:40 +00003256 if (cur->type != XML_DTD_NODE) {
Daniel Veillard5335dc52003-01-01 20:59:38 +00003257
Daniel Veillarda880b122003-04-21 21:36:41 +00003258 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003259 xmlDeregisterNodeDefaultValue(cur);
3260
Daniel Veillard02141ea2001-04-30 11:46:40 +00003261 if ((cur->children != NULL) &&
3262 (cur->type != XML_ENTITY_REF_NODE))
3263 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003264 if (((cur->type == XML_ELEMENT_NODE) ||
3265 (cur->type == XML_XINCLUDE_START) ||
3266 (cur->type == XML_XINCLUDE_END)) &&
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003267 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003268 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003269 if ((cur->type != XML_ELEMENT_NODE) &&
3270 (cur->type != XML_XINCLUDE_START) &&
3271 (cur->type != XML_XINCLUDE_END) &&
3272 (cur->type != XML_ENTITY_REF_NODE)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003273 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003274 }
3275 if (((cur->type == XML_ELEMENT_NODE) ||
3276 (cur->type == XML_XINCLUDE_START) ||
3277 (cur->type == XML_XINCLUDE_END)) &&
3278 (cur->nsDef != NULL))
3279 xmlFreeNsList(cur->nsDef);
3280
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003281 /*
3282 * When a node is a text node or a comment, it uses a global static
3283 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003284 * Otherwise the node name might come from the document's
3285 * dictionnary
Daniel Veillard9cc6dc62001-06-11 08:09:20 +00003286 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00003287 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003288 (cur->type != XML_TEXT_NODE) &&
3289 (cur->type != XML_COMMENT_NODE))
3290 DICT_FREE(cur->name)
Daniel Veillard02141ea2001-04-30 11:46:40 +00003291 xmlFree(cur);
3292 }
Owen Taylor3473f882001-02-23 17:55:21 +00003293 cur = next;
3294 }
3295}
3296
3297/**
3298 * xmlFreeNode:
3299 * @cur: the node
3300 *
3301 * Free a node, this is a recursive behaviour, all the children are freed too.
3302 * This doesn't unlink the child from the list, use xmlUnlinkNode() first.
3303 */
3304void
3305xmlFreeNode(xmlNodePtr cur) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003306 xmlDictPtr dict = NULL;
3307
3308 if (cur == NULL) return;
Daniel Veillard5335dc52003-01-01 20:59:38 +00003309
Daniel Veillard02141ea2001-04-30 11:46:40 +00003310 /* use xmlFreeDtd for DTD nodes */
Daniel Veillarde6a55192002-01-14 17:11:53 +00003311 if (cur->type == XML_DTD_NODE) {
3312 xmlFreeDtd((xmlDtdPtr) cur);
Owen Taylor3473f882001-02-23 17:55:21 +00003313 return;
Daniel Veillarde6a55192002-01-14 17:11:53 +00003314 }
3315 if (cur->type == XML_NAMESPACE_DECL) {
3316 xmlFreeNs((xmlNsPtr) cur);
3317 return;
3318 }
Daniel Veillarda70d62f2002-11-07 14:18:03 +00003319 if (cur->type == XML_ATTRIBUTE_NODE) {
3320 xmlFreeProp((xmlAttrPtr) cur);
3321 return;
3322 }
Daniel Veillard5335dc52003-01-01 20:59:38 +00003323
Daniel Veillarda880b122003-04-21 21:36:41 +00003324 if ((__xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue))
Daniel Veillard5335dc52003-01-01 20:59:38 +00003325 xmlDeregisterNodeDefaultValue(cur);
3326
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003327 if (cur->doc != NULL) dict = cur->doc->dict;
3328
Owen Taylor3473f882001-02-23 17:55:21 +00003329 if ((cur->children != NULL) &&
3330 (cur->type != XML_ENTITY_REF_NODE))
3331 xmlFreeNodeList(cur->children);
Daniel Veillard01c13b52002-12-10 15:19:08 +00003332 if (((cur->type == XML_ELEMENT_NODE) ||
3333 (cur->type == XML_XINCLUDE_START) ||
3334 (cur->type == XML_XINCLUDE_END)) &&
3335 (cur->properties != NULL))
Daniel Veillard02141ea2001-04-30 11:46:40 +00003336 xmlFreePropList(cur->properties);
Daniel Veillard7db37732001-07-12 01:20:08 +00003337 if ((cur->type != XML_ELEMENT_NODE) &&
3338 (cur->content != NULL) &&
3339 (cur->type != XML_ENTITY_REF_NODE) &&
3340 (cur->type != XML_XINCLUDE_END) &&
3341 (cur->type != XML_XINCLUDE_START)) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003342 DICT_FREE(cur->content)
Daniel Veillard7db37732001-07-12 01:20:08 +00003343 }
3344
Daniel Veillardacd370f2001-06-09 17:17:51 +00003345 /*
3346 * When a node is a text node or a comment, it uses a global static
3347 * variable for the name of the node.
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003348 * Otherwise the node name might come from the document's dictionnary
Daniel Veillardacd370f2001-06-09 17:17:51 +00003349 */
Owen Taylor3473f882001-02-23 17:55:21 +00003350 if ((cur->name != NULL) &&
Daniel Veillarde96a2a42003-09-24 21:23:56 +00003351 (cur->type != XML_TEXT_NODE) &&
3352 (cur->type != XML_COMMENT_NODE))
3353 DICT_FREE(cur->name)
Daniel Veillardacd370f2001-06-09 17:17:51 +00003354
Daniel Veillarde1ca5032002-12-09 14:13:43 +00003355 if (((cur->type == XML_ELEMENT_NODE) ||
3356 (cur->type == XML_XINCLUDE_START) ||
3357 (cur->type == XML_XINCLUDE_END)) &&
3358 (cur->nsDef != NULL))
3359 xmlFreeNsList(cur->nsDef);
Owen Taylor3473f882001-02-23 17:55:21 +00003360 xmlFree(cur);
3361}
3362
3363/**
3364 * xmlUnlinkNode:
3365 * @cur: the node
3366 *
3367 * Unlink a node from it's current context, the node is not freed
3368 */
3369void
3370xmlUnlinkNode(xmlNodePtr cur) {
3371 if (cur == NULL) {
3372#ifdef DEBUG_TREE
3373 xmlGenericError(xmlGenericErrorContext,
3374 "xmlUnlinkNode : node == NULL\n");
3375#endif
3376 return;
3377 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003378 if (cur->type == XML_DTD_NODE) {
3379 xmlDocPtr doc;
3380 doc = cur->doc;
Daniel Veillarda067e652003-05-01 08:03:46 +00003381 if (doc != NULL) {
3382 if (doc->intSubset == (xmlDtdPtr) cur)
3383 doc->intSubset = NULL;
3384 if (doc->extSubset == (xmlDtdPtr) cur)
3385 doc->extSubset = NULL;
3386 }
Daniel Veillard29e43992001-12-13 22:21:58 +00003387 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003388 if (cur->parent != NULL) {
3389 xmlNodePtr parent;
3390 parent = cur->parent;
3391 if (cur->type == XML_ATTRIBUTE_NODE) {
3392 if (parent->properties == (xmlAttrPtr) cur)
3393 parent->properties = ((xmlAttrPtr) cur)->next;
3394 } else {
3395 if (parent->children == cur)
3396 parent->children = cur->next;
3397 if (parent->last == cur)
3398 parent->last = cur->prev;
3399 }
3400 cur->parent = NULL;
3401 }
Owen Taylor3473f882001-02-23 17:55:21 +00003402 if (cur->next != NULL)
3403 cur->next->prev = cur->prev;
3404 if (cur->prev != NULL)
3405 cur->prev->next = cur->next;
3406 cur->next = cur->prev = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003407}
3408
Daniel Veillard652327a2003-09-29 18:02:38 +00003409#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003410/**
3411 * xmlReplaceNode:
3412 * @old: the old node
3413 * @cur: the node
3414 *
3415 * Unlink the old node from it's current context, prune the new one
Daniel Veillardd1640922001-12-17 15:30:10 +00003416 * at the same place. If @cur was already inserted in a document it is
Owen Taylor3473f882001-02-23 17:55:21 +00003417 * first unlinked from its existing context.
3418 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003419 * Returns the @old node
Owen Taylor3473f882001-02-23 17:55:21 +00003420 */
3421xmlNodePtr
3422xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) {
3423 if (old == NULL) {
3424#ifdef DEBUG_TREE
3425 xmlGenericError(xmlGenericErrorContext,
3426 "xmlReplaceNode : old == NULL\n");
3427#endif
3428 return(NULL);
3429 }
3430 if (cur == NULL) {
3431 xmlUnlinkNode(old);
3432 return(old);
3433 }
3434 if (cur == old) {
3435 return(old);
3436 }
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003437 if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) {
3438#ifdef DEBUG_TREE
3439 xmlGenericError(xmlGenericErrorContext,
3440 "xmlReplaceNode : Trying to replace attribute node with other node type\n");
3441#endif
3442 return(old);
3443 }
3444 if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) {
3445#ifdef DEBUG_TREE
3446 xmlGenericError(xmlGenericErrorContext,
3447 "xmlReplaceNode : Trying to replace a non-attribute node with attribute node\n");
3448#endif
3449 return(old);
3450 }
Owen Taylor3473f882001-02-23 17:55:21 +00003451 xmlUnlinkNode(cur);
3452 cur->doc = old->doc;
3453 cur->parent = old->parent;
3454 cur->next = old->next;
3455 if (cur->next != NULL)
3456 cur->next->prev = cur;
3457 cur->prev = old->prev;
3458 if (cur->prev != NULL)
3459 cur->prev->next = cur;
3460 if (cur->parent != NULL) {
Daniel Veillardc169f8b2002-01-22 21:40:13 +00003461 if (cur->type == XML_ATTRIBUTE_NODE) {
3462 if (cur->parent->properties == (xmlAttrPtr)old)
3463 cur->parent->properties = ((xmlAttrPtr) cur);
3464 } else {
3465 if (cur->parent->children == old)
3466 cur->parent->children = cur;
3467 if (cur->parent->last == old)
3468 cur->parent->last = cur;
3469 }
Owen Taylor3473f882001-02-23 17:55:21 +00003470 }
3471 old->next = old->prev = NULL;
3472 old->parent = NULL;
3473 return(old);
3474}
Daniel Veillard652327a2003-09-29 18:02:38 +00003475#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003476
3477/************************************************************************
3478 * *
3479 * Copy operations *
3480 * *
3481 ************************************************************************/
3482
3483/**
3484 * xmlCopyNamespace:
3485 * @cur: the namespace
3486 *
3487 * Do a copy of the namespace.
3488 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003489 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003490 */
3491xmlNsPtr
3492xmlCopyNamespace(xmlNsPtr cur) {
3493 xmlNsPtr ret;
3494
3495 if (cur == NULL) return(NULL);
3496 switch (cur->type) {
3497 case XML_LOCAL_NAMESPACE:
3498 ret = xmlNewNs(NULL, cur->href, cur->prefix);
3499 break;
3500 default:
3501#ifdef DEBUG_TREE
3502 xmlGenericError(xmlGenericErrorContext,
3503 "xmlCopyNamespace: invalid type %d\n", cur->type);
3504#endif
3505 return(NULL);
3506 }
3507 return(ret);
3508}
3509
3510/**
3511 * xmlCopyNamespaceList:
3512 * @cur: the first namespace
3513 *
3514 * Do a copy of an namespace list.
3515 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003516 * Returns: a new #xmlNsPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003517 */
3518xmlNsPtr
3519xmlCopyNamespaceList(xmlNsPtr cur) {
3520 xmlNsPtr ret = NULL;
3521 xmlNsPtr p = NULL,q;
3522
3523 while (cur != NULL) {
3524 q = xmlCopyNamespace(cur);
3525 if (p == NULL) {
3526 ret = p = q;
3527 } else {
3528 p->next = q;
3529 p = q;
3530 }
3531 cur = cur->next;
3532 }
3533 return(ret);
3534}
3535
3536static xmlNodePtr
3537xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent);
3538/**
3539 * xmlCopyProp:
3540 * @target: the element where the attribute will be grafted
3541 * @cur: the attribute
3542 *
3543 * Do a copy of the attribute.
3544 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003545 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003546 */
3547xmlAttrPtr
3548xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) {
3549 xmlAttrPtr ret;
3550
3551 if (cur == NULL) return(NULL);
3552 if (target != NULL)
3553 ret = xmlNewDocProp(target->doc, cur->name, NULL);
3554 else if (cur->parent != NULL)
3555 ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL);
3556 else if (cur->children != NULL)
3557 ret = xmlNewDocProp(cur->children->doc, cur->name, NULL);
3558 else
3559 ret = xmlNewDocProp(NULL, cur->name, NULL);
3560 if (ret == NULL) return(NULL);
3561 ret->parent = target;
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003562
Owen Taylor3473f882001-02-23 17:55:21 +00003563 if ((cur->ns != NULL) && (target != NULL)) {
Daniel Veillard8107a222002-01-13 14:10:10 +00003564 xmlNsPtr ns;
Daniel Veillard652327a2003-09-29 18:02:38 +00003565
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003566 ns = xmlSearchNs(target->doc, target, cur->ns->prefix);
3567 if (ns == NULL) {
3568 /*
3569 * Humm, we are copying an element whose namespace is defined
3570 * out of the new tree scope. Search it in the original tree
3571 * and add it at the top of the new tree
3572 */
3573 ns = xmlSearchNs(cur->doc, cur->parent, cur->ns->prefix);
3574 if (ns != NULL) {
3575 xmlNodePtr root = target;
3576 xmlNodePtr pred = NULL;
3577
3578 while (root->parent != NULL) {
3579 pred = root;
3580 root = root->parent;
3581 }
3582 if (root == (xmlNodePtr) target->doc) {
3583 /* correct possibly cycling above the document elt */
3584 root = pred;
3585 }
3586 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
3587 }
3588 } else {
3589 /*
3590 * we have to find something appropriate here since
3591 * we cant be sure, that the namespce we found is identified
3592 * by the prefix
3593 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00003594 if (xmlStrEqual(ns->href, cur->ns->href)) {
Daniel Veillardd4f41aa2002-03-03 14:13:46 +00003595 /* this is the nice case */
3596 ret->ns = ns;
3597 } else {
3598 /*
3599 * we are in trouble: we need a new reconcilied namespace.
3600 * This is expensive
3601 */
3602 ret->ns = xmlNewReconciliedNs(target->doc, target, cur->ns);
3603 }
3604 }
3605
Owen Taylor3473f882001-02-23 17:55:21 +00003606 } else
3607 ret->ns = NULL;
3608
3609 if (cur->children != NULL) {
3610 xmlNodePtr tmp;
3611
3612 ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret);
3613 ret->last = NULL;
3614 tmp = ret->children;
3615 while (tmp != NULL) {
3616 /* tmp->parent = (xmlNodePtr)ret; */
3617 if (tmp->next == NULL)
3618 ret->last = tmp;
3619 tmp = tmp->next;
3620 }
3621 }
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003622 /*
3623 * Try to handle IDs
3624 */
Daniel Veillarda3db2e32002-03-08 15:46:57 +00003625 if ((target!= NULL) && (cur!= NULL) &&
3626 (target->doc != NULL) && (cur->doc != NULL) &&
Daniel Veillardc5f05ad2002-02-10 11:57:22 +00003627 (cur->doc->ids != NULL) && (cur->parent != NULL)) {
3628 if (xmlIsID(cur->doc, cur->parent, cur)) {
3629 xmlChar *id;
3630
3631 id = xmlNodeListGetString(cur->doc, cur->children, 1);
3632 if (id != NULL) {
3633 xmlAddID(NULL, target->doc, id, ret);
3634 xmlFree(id);
3635 }
3636 }
3637 }
Owen Taylor3473f882001-02-23 17:55:21 +00003638 return(ret);
3639}
3640
3641/**
3642 * xmlCopyPropList:
3643 * @target: the element where the attributes will be grafted
3644 * @cur: the first attribute
3645 *
3646 * Do a copy of an attribute list.
3647 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003648 * Returns: a new #xmlAttrPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003649 */
3650xmlAttrPtr
3651xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) {
3652 xmlAttrPtr ret = NULL;
3653 xmlAttrPtr p = NULL,q;
3654
3655 while (cur != NULL) {
3656 q = xmlCopyProp(target, cur);
3657 if (p == NULL) {
3658 ret = p = q;
3659 } else {
3660 p->next = q;
3661 q->prev = p;
3662 p = q;
3663 }
3664 cur = cur->next;
3665 }
3666 return(ret);
3667}
3668
3669/*
Daniel Veillardd1640922001-12-17 15:30:10 +00003670 * NOTE about the CopyNode operations !
Owen Taylor3473f882001-02-23 17:55:21 +00003671 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003672 * They are split into external and internal parts for one
Owen Taylor3473f882001-02-23 17:55:21 +00003673 * tricky reason: namespaces. Doing a direct copy of a node
3674 * say RPM:Copyright without changing the namespace pointer to
3675 * something else can produce stale links. One way to do it is
3676 * to keep a reference counter but this doesn't work as soon
3677 * as one move the element or the subtree out of the scope of
3678 * the existing namespace. The actual solution seems to add
3679 * a copy of the namespace at the top of the copied tree if
3680 * not available in the subtree.
3681 * Hence two functions, the public front-end call the inner ones
3682 */
3683
3684static xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003685xmlStaticCopyNode(const xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent,
Owen Taylor3473f882001-02-23 17:55:21 +00003686 int recursive) {
3687 xmlNodePtr ret;
3688
3689 if (node == NULL) return(NULL);
Daniel Veillard39196eb2001-06-19 18:09:42 +00003690 switch (node->type) {
3691 case XML_TEXT_NODE:
3692 case XML_CDATA_SECTION_NODE:
3693 case XML_ELEMENT_NODE:
Daniel Veillardec6725e2002-09-05 11:12:45 +00003694 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003695 case XML_ENTITY_REF_NODE:
3696 case XML_ENTITY_NODE:
3697 case XML_PI_NODE:
3698 case XML_COMMENT_NODE:
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003699 case XML_XINCLUDE_START:
3700 case XML_XINCLUDE_END:
3701 break;
3702 case XML_ATTRIBUTE_NODE:
3703 return((xmlNodePtr) xmlCopyProp(parent, (xmlAttrPtr) node));
3704 case XML_NAMESPACE_DECL:
3705 return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node));
3706
Daniel Veillard39196eb2001-06-19 18:09:42 +00003707 case XML_DOCUMENT_NODE:
3708 case XML_HTML_DOCUMENT_NODE:
3709#ifdef LIBXML_DOCB_ENABLED
3710 case XML_DOCB_DOCUMENT_NODE:
3711#endif
Daniel Veillard652327a2003-09-29 18:02:38 +00003712#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003713 return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, recursive));
Daniel Veillard652327a2003-09-29 18:02:38 +00003714#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard39196eb2001-06-19 18:09:42 +00003715 case XML_DOCUMENT_TYPE_NODE:
Daniel Veillard39196eb2001-06-19 18:09:42 +00003716 case XML_NOTATION_NODE:
3717 case XML_DTD_NODE:
3718 case XML_ELEMENT_DECL:
3719 case XML_ATTRIBUTE_DECL:
3720 case XML_ENTITY_DECL:
3721 return(NULL);
3722 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003723
Owen Taylor3473f882001-02-23 17:55:21 +00003724 /*
3725 * Allocate a new node and fill the fields.
3726 */
3727 ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
3728 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00003729 xmlTreeErrMemory("copying node");
Owen Taylor3473f882001-02-23 17:55:21 +00003730 return(NULL);
3731 }
3732 memset(ret, 0, sizeof(xmlNode));
3733 ret->type = node->type;
3734
3735 ret->doc = doc;
3736 ret->parent = parent;
3737 if (node->name == xmlStringText)
3738 ret->name = xmlStringText;
3739 else if (node->name == xmlStringTextNoenc)
3740 ret->name = xmlStringTextNoenc;
3741 else if (node->name == xmlStringComment)
3742 ret->name = xmlStringComment;
3743 else if (node->name != NULL)
3744 ret->name = xmlStrdup(node->name);
Daniel Veillard7db37732001-07-12 01:20:08 +00003745 if ((node->type != XML_ELEMENT_NODE) &&
3746 (node->content != NULL) &&
3747 (node->type != XML_ENTITY_REF_NODE) &&
3748 (node->type != XML_XINCLUDE_END) &&
3749 (node->type != XML_XINCLUDE_START)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003750 ret->content = xmlStrdup(node->content);
Daniel Veillard8107a222002-01-13 14:10:10 +00003751 }else{
3752 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00003753 ret->line = node->line;
Owen Taylor3473f882001-02-23 17:55:21 +00003754 }
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003755 if (parent != NULL) {
3756 xmlNodePtr tmp;
3757
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003758 /*
3759 * this is a tricky part for the node register thing:
3760 * in case ret does get coalesced in xmlAddChild
3761 * the deregister-node callback is called; so we register ret now already
3762 */
Daniel Veillarda880b122003-04-21 21:36:41 +00003763 if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003764 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
3765
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003766 tmp = xmlAddChild(parent, ret);
3767 /* node could have coalesced */
3768 if (tmp != ret)
3769 return(tmp);
3770 }
Owen Taylor3473f882001-02-23 17:55:21 +00003771
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003772 if (!recursive)
3773 goto out;
Owen Taylor3473f882001-02-23 17:55:21 +00003774 if (node->nsDef != NULL)
3775 ret->nsDef = xmlCopyNamespaceList(node->nsDef);
3776
3777 if (node->ns != NULL) {
3778 xmlNsPtr ns;
3779
3780 ns = xmlSearchNs(doc, ret, node->ns->prefix);
3781 if (ns == NULL) {
3782 /*
3783 * Humm, we are copying an element whose namespace is defined
3784 * out of the new tree scope. Search it in the original tree
3785 * and add it at the top of the new tree
3786 */
3787 ns = xmlSearchNs(node->doc, node, node->ns->prefix);
3788 if (ns != NULL) {
3789 xmlNodePtr root = ret;
3790
3791 while (root->parent != NULL) root = root->parent;
Daniel Veillarde82a9922001-04-22 12:12:58 +00003792 ret->ns = xmlNewNs(root, ns->href, ns->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 }
3794 } else {
3795 /*
3796 * reference the existing namespace definition in our own tree.
3797 */
3798 ret->ns = ns;
3799 }
3800 }
3801 if (node->properties != NULL)
3802 ret->properties = xmlCopyPropList(ret, node->properties);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003803 if (node->type == XML_ENTITY_REF_NODE) {
3804 if ((doc == NULL) || (node->doc != doc)) {
3805 /*
3806 * The copied node will go into a separate document, so
Daniel Veillardd1640922001-12-17 15:30:10 +00003807 * to avoid dangling references to the ENTITY_DECL node
Daniel Veillardb33c2012001-04-25 12:59:04 +00003808 * we cannot keep the reference. Try to find it in the
3809 * target document.
3810 */
3811 ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name);
3812 } else {
3813 ret->children = node->children;
3814 }
Daniel Veillard0ec98632001-11-14 15:04:32 +00003815 ret->last = ret->children;
3816 } else if (node->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003817 ret->children = xmlStaticCopyNodeList(node->children, doc, ret);
Daniel Veillard0ec98632001-11-14 15:04:32 +00003818 UPDATE_LAST_CHILD_AND_PARENT(ret)
3819 }
Daniel Veillard8a1b1852003-01-05 22:37:17 +00003820
3821out:
3822 /* if parent != NULL we already registered the node above */
3823 if (parent == NULL && xmlRegisterNodeDefaultValue)
3824 xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003825 return(ret);
3826}
3827
3828static xmlNodePtr
3829xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
3830 xmlNodePtr ret = NULL;
3831 xmlNodePtr p = NULL,q;
3832
3833 while (node != NULL) {
Daniel Veillard652327a2003-09-29 18:02:38 +00003834#ifdef LIBXML_TREE_ENABLED
Daniel Veillard1d0bfab2001-07-26 11:49:41 +00003835 if (node->type == XML_DTD_NODE ) {
Daniel Veillard4497e692001-06-09 14:19:02 +00003836 if (doc == NULL) {
3837 node = node->next;
3838 continue;
3839 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00003840 if (doc->intSubset == NULL) {
3841 q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
3842 q->doc = doc;
3843 q->parent = parent;
3844 doc->intSubset = (xmlDtdPtr) q;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003845 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003846 } else {
3847 q = (xmlNodePtr) doc->intSubset;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003848 xmlAddChild(parent, q);
Daniel Veillardb33c2012001-04-25 12:59:04 +00003849 }
3850 } else
Daniel Veillard652327a2003-09-29 18:02:38 +00003851#endif /* LIBXML_TREE_ENABLED */
Daniel Veillardb33c2012001-04-25 12:59:04 +00003852 q = xmlStaticCopyNode(node, doc, parent, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003853 if (ret == NULL) {
3854 q->prev = NULL;
3855 ret = p = q;
Daniel Veillardacb2bda2002-01-13 16:15:43 +00003856 } else if (p != q) {
3857 /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */
Owen Taylor3473f882001-02-23 17:55:21 +00003858 p->next = q;
3859 q->prev = p;
3860 p = q;
3861 }
3862 node = node->next;
3863 }
3864 return(ret);
3865}
3866
3867/**
3868 * xmlCopyNode:
3869 * @node: the node
3870 * @recursive: if 1 do a recursive copy.
3871 *
3872 * Do a copy of the node.
3873 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003874 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003875 */
3876xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003877xmlCopyNode(const xmlNodePtr node, int recursive) {
Owen Taylor3473f882001-02-23 17:55:21 +00003878 xmlNodePtr ret;
3879
3880 ret = xmlStaticCopyNode(node, NULL, NULL, recursive);
3881 return(ret);
3882}
3883
3884/**
Daniel Veillard82daa812001-04-12 08:55:36 +00003885 * xmlDocCopyNode:
3886 * @node: the node
Daniel Veillardd1640922001-12-17 15:30:10 +00003887 * @doc: the document
Daniel Veillard82daa812001-04-12 08:55:36 +00003888 * @recursive: if 1 do a recursive copy.
3889 *
3890 * Do a copy of the node to a given document.
3891 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003892 * Returns: a new #xmlNodePtr, or NULL in case of error.
Daniel Veillard82daa812001-04-12 08:55:36 +00003893 */
3894xmlNodePtr
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003895xmlDocCopyNode(const xmlNodePtr node, xmlDocPtr doc, int recursive) {
Daniel Veillard82daa812001-04-12 08:55:36 +00003896 xmlNodePtr ret;
3897
3898 ret = xmlStaticCopyNode(node, doc, NULL, recursive);
3899 return(ret);
3900}
3901
3902/**
Owen Taylor3473f882001-02-23 17:55:21 +00003903 * xmlCopyNodeList:
3904 * @node: the first node in the list.
3905 *
3906 * Do a recursive copy of the node list.
3907 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003908 * Returns: a new #xmlNodePtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003909 */
Daniel Veillard3ec4c612001-08-28 20:39:49 +00003910xmlNodePtr xmlCopyNodeList(const xmlNodePtr node) {
Owen Taylor3473f882001-02-23 17:55:21 +00003911 xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL);
3912 return(ret);
3913}
3914
Daniel Veillard652327a2003-09-29 18:02:38 +00003915#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003916/**
Owen Taylor3473f882001-02-23 17:55:21 +00003917 * xmlCopyDtd:
3918 * @dtd: the dtd
3919 *
3920 * Do a copy of the dtd.
3921 *
Daniel Veillardd1640922001-12-17 15:30:10 +00003922 * Returns: a new #xmlDtdPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00003923 */
3924xmlDtdPtr
3925xmlCopyDtd(xmlDtdPtr dtd) {
3926 xmlDtdPtr ret;
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003927 xmlNodePtr cur, p = NULL, q;
Owen Taylor3473f882001-02-23 17:55:21 +00003928
3929 if (dtd == NULL) return(NULL);
3930 ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID);
3931 if (ret == NULL) return(NULL);
3932 if (dtd->entities != NULL)
3933 ret->entities = (void *) xmlCopyEntitiesTable(
3934 (xmlEntitiesTablePtr) dtd->entities);
3935 if (dtd->notations != NULL)
3936 ret->notations = (void *) xmlCopyNotationTable(
3937 (xmlNotationTablePtr) dtd->notations);
3938 if (dtd->elements != NULL)
3939 ret->elements = (void *) xmlCopyElementTable(
3940 (xmlElementTablePtr) dtd->elements);
3941 if (dtd->attributes != NULL)
3942 ret->attributes = (void *) xmlCopyAttributeTable(
3943 (xmlAttributeTablePtr) dtd->attributes);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00003944 if (dtd->pentities != NULL)
3945 ret->pentities = (void *) xmlCopyEntitiesTable(
3946 (xmlEntitiesTablePtr) dtd->pentities);
3947
3948 cur = dtd->children;
3949 while (cur != NULL) {
3950 q = NULL;
3951
3952 if (cur->type == XML_ENTITY_DECL) {
3953 xmlEntityPtr tmp = (xmlEntityPtr) cur;
3954 switch (tmp->etype) {
3955 case XML_INTERNAL_GENERAL_ENTITY:
3956 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
3957 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
3958 q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name);
3959 break;
3960 case XML_INTERNAL_PARAMETER_ENTITY:
3961 case XML_EXTERNAL_PARAMETER_ENTITY:
3962 q = (xmlNodePtr)
3963 xmlGetParameterEntityFromDtd(ret, tmp->name);
3964 break;
3965 case XML_INTERNAL_PREDEFINED_ENTITY:
3966 break;
3967 }
3968 } else if (cur->type == XML_ELEMENT_DECL) {
3969 xmlElementPtr tmp = (xmlElementPtr) cur;
3970 q = (xmlNodePtr)
3971 xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix);
3972 } else if (cur->type == XML_ATTRIBUTE_DECL) {
3973 xmlAttributePtr tmp = (xmlAttributePtr) cur;
3974 q = (xmlNodePtr)
3975 xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix);
3976 } else if (cur->type == XML_COMMENT_NODE) {
3977 q = xmlCopyNode(cur, 0);
3978 }
3979
3980 if (q == NULL) {
3981 cur = cur->next;
3982 continue;
3983 }
3984
3985 if (p == NULL)
3986 ret->children = q;
3987 else
3988 p->next = q;
3989
3990 q->prev = p;
3991 q->parent = (xmlNodePtr) ret;
3992 q->next = NULL;
3993 ret->last = q;
3994 p = q;
3995 cur = cur->next;
3996 }
3997
Owen Taylor3473f882001-02-23 17:55:21 +00003998 return(ret);
3999}
4000
4001/**
4002 * xmlCopyDoc:
4003 * @doc: the document
4004 * @recursive: if 1 do a recursive copy.
4005 *
4006 * Do a copy of the document info. If recursive, the content tree will
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004007 * be copied too as well as DTD, namespaces and entities.
Owen Taylor3473f882001-02-23 17:55:21 +00004008 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004009 * Returns: a new #xmlDocPtr, or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00004010 */
4011xmlDocPtr
4012xmlCopyDoc(xmlDocPtr doc, int recursive) {
4013 xmlDocPtr ret;
4014
4015 if (doc == NULL) return(NULL);
4016 ret = xmlNewDoc(doc->version);
4017 if (ret == NULL) return(NULL);
4018 if (doc->name != NULL)
4019 ret->name = xmlMemStrdup(doc->name);
4020 if (doc->encoding != NULL)
4021 ret->encoding = xmlStrdup(doc->encoding);
4022 ret->charset = doc->charset;
4023 ret->compression = doc->compression;
4024 ret->standalone = doc->standalone;
4025 if (!recursive) return(ret);
4026
Daniel Veillardb33c2012001-04-25 12:59:04 +00004027 ret->last = NULL;
4028 ret->children = NULL;
4029 if (doc->intSubset != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004030 ret->intSubset = xmlCopyDtd(doc->intSubset);
Daniel Veillard8ee9c8f2002-01-26 21:42:58 +00004031 xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret);
Daniel Veillardb33c2012001-04-25 12:59:04 +00004032 ret->intSubset->parent = ret;
4033 }
Owen Taylor3473f882001-02-23 17:55:21 +00004034 if (doc->oldNs != NULL)
4035 ret->oldNs = xmlCopyNamespaceList(doc->oldNs);
4036 if (doc->children != NULL) {
4037 xmlNodePtr tmp;
Daniel Veillardb33c2012001-04-25 12:59:04 +00004038
4039 ret->children = xmlStaticCopyNodeList(doc->children, ret,
4040 (xmlNodePtr)ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004041 ret->last = NULL;
4042 tmp = ret->children;
4043 while (tmp != NULL) {
4044 if (tmp->next == NULL)
4045 ret->last = tmp;
4046 tmp = tmp->next;
4047 }
4048 }
4049 return(ret);
4050}
Daniel Veillard652327a2003-09-29 18:02:38 +00004051#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004052
4053/************************************************************************
4054 * *
4055 * Content access functions *
4056 * *
4057 ************************************************************************/
4058
4059/**
Daniel Veillard8faa7832001-11-26 15:58:08 +00004060 * xmlGetLineNo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00004061 * @node: valid node
Daniel Veillard8faa7832001-11-26 15:58:08 +00004062 *
4063 * Get line number of node. this requires activation of this option
Daniel Veillardd1640922001-12-17 15:30:10 +00004064 * before invoking the parser by calling xmlLineNumbersDefault(1)
Daniel Veillard8faa7832001-11-26 15:58:08 +00004065 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004066 * Returns the line number if successful, -1 otherwise
Daniel Veillard8faa7832001-11-26 15:58:08 +00004067 */
4068long
4069xmlGetLineNo(xmlNodePtr node)
4070{
4071 long result = -1;
4072
4073 if (!node)
4074 return result;
4075 if (node->type == XML_ELEMENT_NODE)
Daniel Veillard3e35f8e2003-10-21 00:05:38 +00004076 result = (long) node->line;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004077 else if ((node->prev != NULL) &&
4078 ((node->prev->type == XML_ELEMENT_NODE) ||
4079 (node->prev->type == XML_TEXT_NODE)))
4080 result = xmlGetLineNo(node->prev);
4081 else if ((node->parent != NULL) &&
4082 ((node->parent->type == XML_ELEMENT_NODE) ||
4083 (node->parent->type == XML_TEXT_NODE)))
4084 result = xmlGetLineNo(node->parent);
4085
4086 return result;
4087}
4088
Daniel Veillard652327a2003-09-29 18:02:38 +00004089#ifdef LIBXML_TREE_ENABLED
Daniel Veillard8faa7832001-11-26 15:58:08 +00004090/**
4091 * xmlGetNodePath:
4092 * @node: a node
4093 *
4094 * Build a structure based Path for the given node
4095 *
4096 * Returns the new path or NULL in case of error. The caller must free
4097 * the returned string
4098 */
4099xmlChar *
4100xmlGetNodePath(xmlNodePtr node)
4101{
4102 xmlNodePtr cur, tmp, next;
4103 xmlChar *buffer = NULL, *temp;
4104 size_t buf_len;
4105 xmlChar *buf;
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004106 const char *sep;
Daniel Veillard8faa7832001-11-26 15:58:08 +00004107 const char *name;
4108 char nametemp[100];
4109 int occur = 0;
4110
4111 if (node == NULL)
4112 return (NULL);
4113
4114 buf_len = 500;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004115 buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004116 if (buffer == NULL) {
4117 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004118 return (NULL);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004119 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004120 buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
Daniel Veillard8faa7832001-11-26 15:58:08 +00004121 if (buf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004122 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004123 xmlFree(buffer);
4124 return (NULL);
4125 }
4126
4127 buffer[0] = 0;
4128 cur = node;
4129 do {
4130 name = "";
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004131 sep = "?";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004132 occur = 0;
4133 if ((cur->type == XML_DOCUMENT_NODE) ||
4134 (cur->type == XML_HTML_DOCUMENT_NODE)) {
4135 if (buffer[0] == '/')
4136 break;
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004137 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004138 next = NULL;
4139 } else if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004140 sep = "/";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004141 name = (const char *) cur->name;
4142 if (cur->ns) {
4143 snprintf(nametemp, sizeof(nametemp) - 1,
4144 "%s:%s", cur->ns->prefix, cur->name);
4145 nametemp[sizeof(nametemp) - 1] = 0;
4146 name = nametemp;
4147 }
4148 next = cur->parent;
4149
4150 /*
4151 * Thumbler index computation
Daniel Veillardc00cda82003-04-07 10:22:39 +00004152 * TODO: the ocurence test seems bogus for namespaced names
Daniel Veillard8faa7832001-11-26 15:58:08 +00004153 */
4154 tmp = cur->prev;
4155 while (tmp != NULL) {
Daniel Veillard0f04f8e2002-09-17 23:04:40 +00004156 if ((tmp->type == XML_ELEMENT_NODE) &&
4157 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004158 occur++;
4159 tmp = tmp->prev;
4160 }
4161 if (occur == 0) {
4162 tmp = cur->next;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004163 while (tmp != NULL && occur == 0) {
4164 if ((tmp->type == XML_ELEMENT_NODE) &&
4165 (xmlStrEqual(cur->name, tmp->name)))
Daniel Veillard8faa7832001-11-26 15:58:08 +00004166 occur++;
4167 tmp = tmp->next;
4168 }
4169 if (occur != 0)
4170 occur = 1;
4171 } else
4172 occur++;
Daniel Veillard8606bbb2002-11-12 12:36:52 +00004173 } else if (cur->type == XML_COMMENT_NODE) {
4174 sep = "/";
4175 name = "comment()";
4176 next = cur->parent;
4177
4178 /*
4179 * Thumbler index computation
4180 */
4181 tmp = cur->prev;
4182 while (tmp != NULL) {
4183 if (tmp->type == XML_COMMENT_NODE)
4184 occur++;
4185 tmp = tmp->prev;
4186 }
4187 if (occur == 0) {
4188 tmp = cur->next;
4189 while (tmp != NULL && occur == 0) {
4190 if (tmp->type == XML_COMMENT_NODE)
4191 occur++;
4192 tmp = tmp->next;
4193 }
4194 if (occur != 0)
4195 occur = 1;
4196 } else
4197 occur++;
4198 } else if ((cur->type == XML_TEXT_NODE) ||
4199 (cur->type == XML_CDATA_SECTION_NODE)) {
4200 sep = "/";
4201 name = "text()";
4202 next = cur->parent;
4203
4204 /*
4205 * Thumbler index computation
4206 */
4207 tmp = cur->prev;
4208 while (tmp != NULL) {
4209 if ((cur->type == XML_TEXT_NODE) ||
4210 (cur->type == XML_CDATA_SECTION_NODE))
4211 occur++;
4212 tmp = tmp->prev;
4213 }
4214 if (occur == 0) {
4215 tmp = cur->next;
4216 while (tmp != NULL && occur == 0) {
4217 if ((cur->type == XML_TEXT_NODE) ||
4218 (cur->type == XML_CDATA_SECTION_NODE))
4219 occur++;
4220 tmp = tmp->next;
4221 }
4222 if (occur != 0)
4223 occur = 1;
4224 } else
4225 occur++;
4226 } else if (cur->type == XML_PI_NODE) {
4227 sep = "/";
4228 snprintf(nametemp, sizeof(nametemp) - 1,
4229 "processing-instruction('%s')", cur->name);
4230 nametemp[sizeof(nametemp) - 1] = 0;
4231 name = nametemp;
4232
4233 next = cur->parent;
4234
4235 /*
4236 * Thumbler index computation
4237 */
4238 tmp = cur->prev;
4239 while (tmp != NULL) {
4240 if ((tmp->type == XML_PI_NODE) &&
4241 (xmlStrEqual(cur->name, tmp->name)))
4242 occur++;
4243 tmp = tmp->prev;
4244 }
4245 if (occur == 0) {
4246 tmp = cur->next;
4247 while (tmp != NULL && occur == 0) {
4248 if ((tmp->type == XML_PI_NODE) &&
4249 (xmlStrEqual(cur->name, tmp->name)))
4250 occur++;
4251 tmp = tmp->next;
4252 }
4253 if (occur != 0)
4254 occur = 1;
4255 } else
4256 occur++;
4257
Daniel Veillard8faa7832001-11-26 15:58:08 +00004258 } else if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004259 sep = "/@";
Daniel Veillard8faa7832001-11-26 15:58:08 +00004260 name = (const char *) (((xmlAttrPtr) cur)->name);
4261 next = ((xmlAttrPtr) cur)->parent;
4262 } else {
4263 next = cur->parent;
4264 }
4265
4266 /*
4267 * Make sure there is enough room
4268 */
4269 if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) {
4270 buf_len =
4271 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
4272 temp = (xmlChar *) xmlRealloc(buffer, buf_len);
4273 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004274 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004275 xmlFree(buf);
4276 xmlFree(buffer);
4277 return (NULL);
4278 }
4279 buffer = temp;
4280 temp = (xmlChar *) xmlRealloc(buf, buf_len);
4281 if (temp == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00004282 xmlTreeErrMemory("getting node path");
Daniel Veillard8faa7832001-11-26 15:58:08 +00004283 xmlFree(buf);
4284 xmlFree(buffer);
4285 return (NULL);
4286 }
4287 buf = temp;
4288 }
4289 if (occur == 0)
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004290 snprintf((char *) buf, buf_len, "%s%s%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004291 sep, name, (char *) buffer);
4292 else
Daniel Veillard9dc1cf12002-10-08 08:26:11 +00004293 snprintf((char *) buf, buf_len, "%s%s[%d]%s",
Daniel Veillard8faa7832001-11-26 15:58:08 +00004294 sep, name, occur, (char *) buffer);
4295 snprintf((char *) buffer, buf_len, "%s", buf);
4296 cur = next;
4297 } while (cur != NULL);
4298 xmlFree(buf);
4299 return (buffer);
4300}
Daniel Veillard652327a2003-09-29 18:02:38 +00004301#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard8faa7832001-11-26 15:58:08 +00004302
4303/**
Owen Taylor3473f882001-02-23 17:55:21 +00004304 * xmlDocGetRootElement:
4305 * @doc: the document
4306 *
4307 * Get the root element of the document (doc->children is a list
4308 * containing possibly comments, PIs, etc ...).
4309 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004310 * Returns the #xmlNodePtr for the root or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00004311 */
4312xmlNodePtr
4313xmlDocGetRootElement(xmlDocPtr doc) {
4314 xmlNodePtr ret;
4315
4316 if (doc == NULL) return(NULL);
4317 ret = doc->children;
4318 while (ret != NULL) {
4319 if (ret->type == XML_ELEMENT_NODE)
4320 return(ret);
4321 ret = ret->next;
4322 }
4323 return(ret);
4324}
4325
Daniel Veillard652327a2003-09-29 18:02:38 +00004326#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004327/**
4328 * xmlDocSetRootElement:
4329 * @doc: the document
4330 * @root: the new document root element
4331 *
4332 * Set the root element of the document (doc->children is a list
4333 * containing possibly comments, PIs, etc ...).
4334 *
4335 * Returns the old root element if any was found
4336 */
4337xmlNodePtr
4338xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) {
4339 xmlNodePtr old = NULL;
4340
4341 if (doc == NULL) return(NULL);
Daniel Veillardc575b992002-02-08 13:28:40 +00004342 if (root == NULL)
4343 return(NULL);
4344 xmlUnlinkNode(root);
4345 root->doc = doc;
4346 root->parent = (xmlNodePtr) doc;
Owen Taylor3473f882001-02-23 17:55:21 +00004347 old = doc->children;
4348 while (old != NULL) {
4349 if (old->type == XML_ELEMENT_NODE)
4350 break;
4351 old = old->next;
4352 }
4353 if (old == NULL) {
4354 if (doc->children == NULL) {
4355 doc->children = root;
4356 doc->last = root;
4357 } else {
4358 xmlAddSibling(doc->children, root);
4359 }
4360 } else {
4361 xmlReplaceNode(old, root);
4362 }
4363 return(old);
4364}
4365
4366/**
4367 * xmlNodeSetLang:
4368 * @cur: the node being changed
Daniel Veillardd1640922001-12-17 15:30:10 +00004369 * @lang: the language description
Owen Taylor3473f882001-02-23 17:55:21 +00004370 *
4371 * Set the language of a node, i.e. the values of the xml:lang
4372 * attribute.
4373 */
4374void
4375xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004376 xmlNsPtr ns;
4377
Owen Taylor3473f882001-02-23 17:55:21 +00004378 if (cur == NULL) return;
4379 switch(cur->type) {
4380 case XML_TEXT_NODE:
4381 case XML_CDATA_SECTION_NODE:
4382 case XML_COMMENT_NODE:
4383 case XML_DOCUMENT_NODE:
4384 case XML_DOCUMENT_TYPE_NODE:
4385 case XML_DOCUMENT_FRAG_NODE:
4386 case XML_NOTATION_NODE:
4387 case XML_HTML_DOCUMENT_NODE:
4388 case XML_DTD_NODE:
4389 case XML_ELEMENT_DECL:
4390 case XML_ATTRIBUTE_DECL:
4391 case XML_ENTITY_DECL:
4392 case XML_PI_NODE:
4393 case XML_ENTITY_REF_NODE:
4394 case XML_ENTITY_NODE:
4395 case XML_NAMESPACE_DECL:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004396#ifdef LIBXML_DOCB_ENABLED
4397 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004398#endif
4399 case XML_XINCLUDE_START:
4400 case XML_XINCLUDE_END:
4401 return;
4402 case XML_ELEMENT_NODE:
4403 case XML_ATTRIBUTE_NODE:
4404 break;
4405 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004406 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4407 if (ns == NULL)
4408 return;
4409 xmlSetNsProp(cur, ns, BAD_CAST "lang", lang);
Owen Taylor3473f882001-02-23 17:55:21 +00004410}
Daniel Veillard652327a2003-09-29 18:02:38 +00004411#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004412
4413/**
4414 * xmlNodeGetLang:
4415 * @cur: the node being checked
4416 *
4417 * Searches the language of a node, i.e. the values of the xml:lang
4418 * attribute or the one carried by the nearest ancestor.
4419 *
4420 * Returns a pointer to the lang value, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004421 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004422 */
4423xmlChar *
4424xmlNodeGetLang(xmlNodePtr cur) {
4425 xmlChar *lang;
4426
4427 while (cur != NULL) {
Daniel Veillardc17337c2001-05-09 10:51:31 +00004428 lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004429 if (lang != NULL)
4430 return(lang);
4431 cur = cur->parent;
4432 }
4433 return(NULL);
4434}
4435
4436
Daniel Veillard652327a2003-09-29 18:02:38 +00004437#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004438/**
4439 * xmlNodeSetSpacePreserve:
4440 * @cur: the node being changed
4441 * @val: the xml:space value ("0": default, 1: "preserve")
4442 *
4443 * Set (or reset) the space preserving behaviour of a node, i.e. the
4444 * value of the xml:space attribute.
4445 */
4446void
4447xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004448 xmlNsPtr ns;
4449
Owen Taylor3473f882001-02-23 17:55:21 +00004450 if (cur == NULL) return;
4451 switch(cur->type) {
4452 case XML_TEXT_NODE:
4453 case XML_CDATA_SECTION_NODE:
4454 case XML_COMMENT_NODE:
4455 case XML_DOCUMENT_NODE:
4456 case XML_DOCUMENT_TYPE_NODE:
4457 case XML_DOCUMENT_FRAG_NODE:
4458 case XML_NOTATION_NODE:
4459 case XML_HTML_DOCUMENT_NODE:
4460 case XML_DTD_NODE:
4461 case XML_ELEMENT_DECL:
4462 case XML_ATTRIBUTE_DECL:
4463 case XML_ENTITY_DECL:
4464 case XML_PI_NODE:
4465 case XML_ENTITY_REF_NODE:
4466 case XML_ENTITY_NODE:
4467 case XML_NAMESPACE_DECL:
4468 case XML_XINCLUDE_START:
4469 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004470#ifdef LIBXML_DOCB_ENABLED
4471 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004472#endif
4473 return;
4474 case XML_ELEMENT_NODE:
4475 case XML_ATTRIBUTE_NODE:
4476 break;
4477 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004478 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4479 if (ns == NULL)
4480 return;
Owen Taylor3473f882001-02-23 17:55:21 +00004481 switch (val) {
4482 case 0:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004483 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "default");
Owen Taylor3473f882001-02-23 17:55:21 +00004484 break;
4485 case 1:
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004486 xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST "preserve");
Owen Taylor3473f882001-02-23 17:55:21 +00004487 break;
4488 }
4489}
Daniel Veillard652327a2003-09-29 18:02:38 +00004490#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004491
4492/**
4493 * xmlNodeGetSpacePreserve:
4494 * @cur: the node being checked
4495 *
4496 * Searches the space preserving behaviour of a node, i.e. the values
4497 * of the xml:space attribute or the one carried by the nearest
4498 * ancestor.
4499 *
Daniel Veillardd1640922001-12-17 15:30:10 +00004500 * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve"
Owen Taylor3473f882001-02-23 17:55:21 +00004501 */
4502int
4503xmlNodeGetSpacePreserve(xmlNodePtr cur) {
4504 xmlChar *space;
4505
4506 while (cur != NULL) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004507 space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE);
Owen Taylor3473f882001-02-23 17:55:21 +00004508 if (space != NULL) {
4509 if (xmlStrEqual(space, BAD_CAST "preserve")) {
4510 xmlFree(space);
4511 return(1);
4512 }
4513 if (xmlStrEqual(space, BAD_CAST "default")) {
4514 xmlFree(space);
4515 return(0);
4516 }
4517 xmlFree(space);
4518 }
4519 cur = cur->parent;
4520 }
4521 return(-1);
4522}
4523
Daniel Veillard652327a2003-09-29 18:02:38 +00004524#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00004525/**
4526 * xmlNodeSetName:
4527 * @cur: the node being changed
4528 * @name: the new tag name
4529 *
4530 * Set (or reset) the name of a node.
4531 */
4532void
4533xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
4534 if (cur == NULL) return;
4535 if (name == NULL) return;
4536 switch(cur->type) {
4537 case XML_TEXT_NODE:
4538 case XML_CDATA_SECTION_NODE:
4539 case XML_COMMENT_NODE:
4540 case XML_DOCUMENT_TYPE_NODE:
4541 case XML_DOCUMENT_FRAG_NODE:
4542 case XML_NOTATION_NODE:
4543 case XML_HTML_DOCUMENT_NODE:
4544 case XML_NAMESPACE_DECL:
4545 case XML_XINCLUDE_START:
4546 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004547#ifdef LIBXML_DOCB_ENABLED
4548 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004549#endif
4550 return;
4551 case XML_ELEMENT_NODE:
4552 case XML_ATTRIBUTE_NODE:
4553 case XML_PI_NODE:
4554 case XML_ENTITY_REF_NODE:
4555 case XML_ENTITY_NODE:
4556 case XML_DTD_NODE:
4557 case XML_DOCUMENT_NODE:
4558 case XML_ELEMENT_DECL:
4559 case XML_ATTRIBUTE_DECL:
4560 case XML_ENTITY_DECL:
4561 break;
4562 }
4563 if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
4564 cur->name = xmlStrdup(name);
4565}
4566
4567/**
4568 * xmlNodeSetBase:
4569 * @cur: the node being changed
4570 * @uri: the new base URI
4571 *
4572 * Set (or reset) the base URI of a node, i.e. the value of the
4573 * xml:base attribute.
4574 */
4575void
Daniel Veillardf85ce8e2003-09-22 10:24:45 +00004576xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) {
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004577 xmlNsPtr ns;
4578
Owen Taylor3473f882001-02-23 17:55:21 +00004579 if (cur == NULL) return;
4580 switch(cur->type) {
4581 case XML_TEXT_NODE:
4582 case XML_CDATA_SECTION_NODE:
4583 case XML_COMMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004584 case XML_DOCUMENT_TYPE_NODE:
4585 case XML_DOCUMENT_FRAG_NODE:
4586 case XML_NOTATION_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004587 case XML_DTD_NODE:
4588 case XML_ELEMENT_DECL:
4589 case XML_ATTRIBUTE_DECL:
4590 case XML_ENTITY_DECL:
4591 case XML_PI_NODE:
4592 case XML_ENTITY_REF_NODE:
4593 case XML_ENTITY_NODE:
4594 case XML_NAMESPACE_DECL:
4595 case XML_XINCLUDE_START:
4596 case XML_XINCLUDE_END:
Owen Taylor3473f882001-02-23 17:55:21 +00004597 return;
4598 case XML_ELEMENT_NODE:
4599 case XML_ATTRIBUTE_NODE:
4600 break;
Daniel Veillard4cbe4702002-05-05 06:57:27 +00004601 case XML_DOCUMENT_NODE:
4602#ifdef LIBXML_DOCB_ENABLED
4603 case XML_DOCB_DOCUMENT_NODE:
4604#endif
4605 case XML_HTML_DOCUMENT_NODE: {
4606 xmlDocPtr doc = (xmlDocPtr) cur;
4607
4608 if (doc->URL != NULL)
4609 xmlFree((xmlChar *) doc->URL);
4610 if (uri == NULL)
4611 doc->URL = NULL;
4612 else
4613 doc->URL = xmlStrdup(uri);
4614 return;
4615 }
Owen Taylor3473f882001-02-23 17:55:21 +00004616 }
Daniel Veillardcfa0d812002-01-17 08:46:58 +00004617
4618 ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
4619 if (ns == NULL)
4620 return;
4621 xmlSetNsProp(cur, ns, BAD_CAST "base", uri);
Owen Taylor3473f882001-02-23 17:55:21 +00004622}
Daniel Veillard652327a2003-09-29 18:02:38 +00004623#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00004624
4625/**
Owen Taylor3473f882001-02-23 17:55:21 +00004626 * xmlNodeGetBase:
4627 * @doc: the document the node pertains to
4628 * @cur: the node being checked
4629 *
4630 * Searches for the BASE URL. The code should work on both XML
4631 * and HTML document even if base mechanisms are completely different.
4632 * It returns the base as defined in RFC 2396 sections
4633 * 5.1.1. Base URI within Document Content
4634 * and
4635 * 5.1.2. Base URI from the Encapsulating Entity
4636 * However it does not return the document base (5.1.3), use
4637 * xmlDocumentGetBase() for this
4638 *
4639 * Returns a pointer to the base URL, or NULL if not found
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004640 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004641 */
4642xmlChar *
4643xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004644 xmlChar *oldbase = NULL;
4645 xmlChar *base, *newbase;
Owen Taylor3473f882001-02-23 17:55:21 +00004646
4647 if ((cur == NULL) && (doc == NULL))
4648 return(NULL);
4649 if (doc == NULL) doc = cur->doc;
4650 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
4651 cur = doc->children;
4652 while ((cur != NULL) && (cur->name != NULL)) {
4653 if (cur->type != XML_ELEMENT_NODE) {
4654 cur = cur->next;
4655 continue;
4656 }
4657 if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) {
4658 cur = cur->children;
4659 continue;
4660 }
4661 if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) {
4662 cur = cur->children;
4663 continue;
4664 }
4665 if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) {
4666 return(xmlGetProp(cur, BAD_CAST "href"));
4667 }
4668 cur = cur->next;
4669 }
4670 return(NULL);
4671 }
4672 while (cur != NULL) {
4673 if (cur->type == XML_ENTITY_DECL) {
4674 xmlEntityPtr ent = (xmlEntityPtr) cur;
4675 return(xmlStrdup(ent->URI));
4676 }
Daniel Veillard42596ad2001-05-22 16:57:14 +00004677 if (cur->type == XML_ELEMENT_NODE) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004678 base = xmlGetNsProp(cur, BAD_CAST "base", XML_XML_NAMESPACE);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004679 if (base != NULL) {
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004680 if (oldbase != NULL) {
4681 newbase = xmlBuildURI(oldbase, base);
4682 if (newbase != NULL) {
4683 xmlFree(oldbase);
4684 xmlFree(base);
4685 oldbase = newbase;
4686 } else {
4687 xmlFree(oldbase);
4688 xmlFree(base);
4689 return(NULL);
4690 }
4691 } else {
4692 oldbase = base;
4693 }
4694 if ((!xmlStrncmp(oldbase, BAD_CAST "http://", 7)) ||
4695 (!xmlStrncmp(oldbase, BAD_CAST "ftp://", 6)) ||
4696 (!xmlStrncmp(oldbase, BAD_CAST "urn:", 4)))
4697 return(oldbase);
Daniel Veillard42596ad2001-05-22 16:57:14 +00004698 }
4699 }
Owen Taylor3473f882001-02-23 17:55:21 +00004700 cur = cur->parent;
4701 }
Daniel Veillardb8c9be92001-07-09 16:01:19 +00004702 if ((doc != NULL) && (doc->URL != NULL)) {
4703 if (oldbase == NULL)
4704 return(xmlStrdup(doc->URL));
4705 newbase = xmlBuildURI(oldbase, doc->URL);
4706 xmlFree(oldbase);
4707 return(newbase);
4708 }
4709 return(oldbase);
Owen Taylor3473f882001-02-23 17:55:21 +00004710}
4711
4712/**
Daniel Veillard78697292003-10-19 20:44:43 +00004713 * xmlNodeBufGetContent:
4714 * @buffer: a buffer
4715 * @cur: the node being read
4716 *
4717 * Read the value of a node @cur, this can be either the text carried
4718 * directly by this node if it's a TEXT node or the aggregate string
4719 * of the values carried by this node child's (TEXT and ENTITY_REF).
4720 * Entity references are substituted.
4721 * Fills up the buffer @buffer with this value
4722 *
4723 * Returns 0 in case of success and -1 in case of error.
4724 */
4725int
4726xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur)
4727{
4728 if ((cur == NULL) || (buffer == NULL)) return(-1);
4729 switch (cur->type) {
4730 case XML_CDATA_SECTION_NODE:
4731 case XML_TEXT_NODE:
4732 xmlBufferCat(buffer, cur->content);
4733 break;
4734 case XML_DOCUMENT_FRAG_NODE:
4735 case XML_ELEMENT_NODE:{
4736 xmlNodePtr tmp = cur;
4737
4738 while (tmp != NULL) {
4739 switch (tmp->type) {
4740 case XML_CDATA_SECTION_NODE:
4741 case XML_TEXT_NODE:
4742 if (tmp->content != NULL)
4743 xmlBufferCat(buffer, tmp->content);
4744 break;
4745 case XML_ENTITY_REF_NODE:
4746 xmlNodeBufGetContent(buffer, tmp->children);
4747 break;
4748 default:
4749 break;
4750 }
4751 /*
4752 * Skip to next node
4753 */
4754 if (tmp->children != NULL) {
4755 if (tmp->children->type != XML_ENTITY_DECL) {
4756 tmp = tmp->children;
4757 continue;
4758 }
4759 }
4760 if (tmp == cur)
4761 break;
4762
4763 if (tmp->next != NULL) {
4764 tmp = tmp->next;
4765 continue;
4766 }
4767
4768 do {
4769 tmp = tmp->parent;
4770 if (tmp == NULL)
4771 break;
4772 if (tmp == cur) {
4773 tmp = NULL;
4774 break;
4775 }
4776 if (tmp->next != NULL) {
4777 tmp = tmp->next;
4778 break;
4779 }
4780 } while (tmp != NULL);
4781 }
4782 break;
4783 }
4784 case XML_ATTRIBUTE_NODE:{
4785 xmlAttrPtr attr = (xmlAttrPtr) cur;
4786 xmlNodePtr tmp = attr->children;
4787
4788 while (tmp != NULL) {
4789 if (tmp->type == XML_TEXT_NODE)
4790 xmlBufferCat(buffer, tmp->content);
4791 else
4792 xmlNodeBufGetContent(buffer, tmp);
4793 tmp = tmp->next;
4794 }
4795 break;
4796 }
4797 case XML_COMMENT_NODE:
4798 case XML_PI_NODE:
4799 xmlBufferCat(buffer, cur->content);
4800 break;
4801 case XML_ENTITY_REF_NODE:{
4802 xmlEntityPtr ent;
4803 xmlNodePtr tmp;
4804
4805 /* lookup entity declaration */
4806 ent = xmlGetDocEntity(cur->doc, cur->name);
4807 if (ent == NULL)
4808 return(-1);
4809
4810 /* an entity content can be any "well balanced chunk",
4811 * i.e. the result of the content [43] production:
4812 * http://www.w3.org/TR/REC-xml#NT-content
4813 * -> we iterate through child nodes and recursive call
4814 * xmlNodeGetContent() which handles all possible node types */
4815 tmp = ent->children;
4816 while (tmp) {
4817 xmlNodeBufGetContent(buffer, tmp);
4818 tmp = tmp->next;
4819 }
4820 break;
4821 }
4822 case XML_ENTITY_NODE:
4823 case XML_DOCUMENT_TYPE_NODE:
4824 case XML_NOTATION_NODE:
4825 case XML_DTD_NODE:
4826 case XML_XINCLUDE_START:
4827 case XML_XINCLUDE_END:
4828 break;
4829 case XML_DOCUMENT_NODE:
4830#ifdef LIBXML_DOCB_ENABLED
4831 case XML_DOCB_DOCUMENT_NODE:
4832#endif
4833 case XML_HTML_DOCUMENT_NODE:
4834 cur = cur->children;
4835 while (cur!= NULL) {
4836 if ((cur->type == XML_ELEMENT_NODE) ||
4837 (cur->type == XML_TEXT_NODE) ||
4838 (cur->type == XML_CDATA_SECTION_NODE)) {
4839 xmlNodeBufGetContent(buffer, cur);
4840 }
4841 cur = cur->next;
4842 }
4843 break;
4844 case XML_NAMESPACE_DECL:
4845 xmlBufferCat(buffer, ((xmlNsPtr) cur)->href);
4846 break;
4847 case XML_ELEMENT_DECL:
4848 case XML_ATTRIBUTE_DECL:
4849 case XML_ENTITY_DECL:
4850 break;
4851 }
4852 return(0);
4853}
4854/**
Owen Taylor3473f882001-02-23 17:55:21 +00004855 * xmlNodeGetContent:
4856 * @cur: the node being read
4857 *
4858 * Read the value of a node, this can be either the text carried
4859 * directly by this node if it's a TEXT node or the aggregate string
4860 * of the values carried by this node child's (TEXT and ENTITY_REF).
Daniel Veillardd1640922001-12-17 15:30:10 +00004861 * Entity references are substituted.
4862 * Returns a new #xmlChar * or NULL if no content is available.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004863 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00004864 */
4865xmlChar *
Daniel Veillard7646b182002-04-20 06:41:40 +00004866xmlNodeGetContent(xmlNodePtr cur)
4867{
4868 if (cur == NULL)
4869 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004870 switch (cur->type) {
4871 case XML_DOCUMENT_FRAG_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004872 case XML_ELEMENT_NODE:{
Daniel Veillard7646b182002-04-20 06:41:40 +00004873 xmlBufferPtr buffer;
4874 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004875
Daniel Veillard814a76d2003-01-23 18:24:20 +00004876 buffer = xmlBufferCreateSize(64);
Daniel Veillard7646b182002-04-20 06:41:40 +00004877 if (buffer == NULL)
4878 return (NULL);
Daniel Veillardc4696922003-10-19 21:47:14 +00004879 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004880 ret = buffer->content;
4881 buffer->content = NULL;
4882 xmlBufferFree(buffer);
4883 return (ret);
4884 }
4885 case XML_ATTRIBUTE_NODE:{
4886 xmlAttrPtr attr = (xmlAttrPtr) cur;
4887
4888 if (attr->parent != NULL)
4889 return (xmlNodeListGetString
4890 (attr->parent->doc, attr->children, 1));
4891 else
4892 return (xmlNodeListGetString(NULL, attr->children, 1));
4893 break;
4894 }
Owen Taylor3473f882001-02-23 17:55:21 +00004895 case XML_COMMENT_NODE:
4896 case XML_PI_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004897 if (cur->content != NULL)
4898 return (xmlStrdup(cur->content));
4899 return (NULL);
4900 case XML_ENTITY_REF_NODE:{
4901 xmlEntityPtr ent;
Daniel Veillard7646b182002-04-20 06:41:40 +00004902 xmlBufferPtr buffer;
4903 xmlChar *ret;
4904
4905 /* lookup entity declaration */
4906 ent = xmlGetDocEntity(cur->doc, cur->name);
4907 if (ent == NULL)
4908 return (NULL);
4909
4910 buffer = xmlBufferCreate();
4911 if (buffer == NULL)
4912 return (NULL);
4913
Daniel Veillardc4696922003-10-19 21:47:14 +00004914 xmlNodeBufGetContent(buffer, cur);
Daniel Veillard7646b182002-04-20 06:41:40 +00004915
4916 ret = buffer->content;
4917 buffer->content = NULL;
4918 xmlBufferFree(buffer);
4919 return (ret);
4920 }
Owen Taylor3473f882001-02-23 17:55:21 +00004921 case XML_ENTITY_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004922 case XML_DOCUMENT_TYPE_NODE:
4923 case XML_NOTATION_NODE:
4924 case XML_DTD_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004925 case XML_XINCLUDE_START:
4926 case XML_XINCLUDE_END:
Daniel Veillard9adc0462003-03-24 18:39:54 +00004927 return (NULL);
4928 case XML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004929#ifdef LIBXML_DOCB_ENABLED
Daniel Veillard7646b182002-04-20 06:41:40 +00004930 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004931#endif
Daniel Veillard9adc0462003-03-24 18:39:54 +00004932 case XML_HTML_DOCUMENT_NODE: {
Daniel Veillardc4696922003-10-19 21:47:14 +00004933 xmlBufferPtr buffer;
4934 xmlChar *ret;
Daniel Veillard9adc0462003-03-24 18:39:54 +00004935
Daniel Veillardc4696922003-10-19 21:47:14 +00004936 buffer = xmlBufferCreate();
4937 if (buffer == NULL)
4938 return (NULL);
4939
4940 xmlNodeBufGetContent(buffer, (xmlNodePtr) cur);
4941
4942 ret = buffer->content;
4943 buffer->content = NULL;
4944 xmlBufferFree(buffer);
4945 return (ret);
Daniel Veillard9adc0462003-03-24 18:39:54 +00004946 }
Daniel Veillard96c3a3b2002-10-14 15:39:04 +00004947 case XML_NAMESPACE_DECL: {
4948 xmlChar *tmp;
4949
4950 tmp = xmlStrdup(((xmlNsPtr) cur)->href);
4951 return (tmp);
4952 }
Owen Taylor3473f882001-02-23 17:55:21 +00004953 case XML_ELEMENT_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004954 /* TODO !!! */
4955 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004956 case XML_ATTRIBUTE_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004957 /* TODO !!! */
4958 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004959 case XML_ENTITY_DECL:
Daniel Veillard7646b182002-04-20 06:41:40 +00004960 /* TODO !!! */
4961 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004962 case XML_CDATA_SECTION_NODE:
4963 case XML_TEXT_NODE:
Daniel Veillard7646b182002-04-20 06:41:40 +00004964 if (cur->content != NULL)
4965 return (xmlStrdup(cur->content));
4966 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004967 }
Daniel Veillard7646b182002-04-20 06:41:40 +00004968 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004969}
Daniel Veillard652327a2003-09-29 18:02:38 +00004970
Owen Taylor3473f882001-02-23 17:55:21 +00004971/**
4972 * xmlNodeSetContent:
4973 * @cur: the node being modified
4974 * @content: the new value of the content
4975 *
4976 * Replace the content of a node.
4977 */
4978void
4979xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) {
4980 if (cur == NULL) {
4981#ifdef DEBUG_TREE
4982 xmlGenericError(xmlGenericErrorContext,
4983 "xmlNodeSetContent : node == NULL\n");
4984#endif
4985 return;
4986 }
4987 switch (cur->type) {
4988 case XML_DOCUMENT_FRAG_NODE:
4989 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00004990 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004991 if (cur->children != NULL) xmlFreeNodeList(cur->children);
4992 cur->children = xmlStringGetNodeList(cur->doc, content);
4993 UPDATE_LAST_CHILD_AND_PARENT(cur)
4994 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004995 case XML_TEXT_NODE:
4996 case XML_CDATA_SECTION_NODE:
4997 case XML_ENTITY_REF_NODE:
4998 case XML_ENTITY_NODE:
4999 case XML_PI_NODE:
5000 case XML_COMMENT_NODE:
5001 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005002 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005003 }
5004 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5005 cur->last = cur->children = NULL;
5006 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005007 cur->content = xmlStrdup(content);
Owen Taylor3473f882001-02-23 17:55:21 +00005008 } else
5009 cur->content = NULL;
5010 break;
5011 case XML_DOCUMENT_NODE:
5012 case XML_HTML_DOCUMENT_NODE:
5013 case XML_DOCUMENT_TYPE_NODE:
5014 case XML_XINCLUDE_START:
5015 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005016#ifdef LIBXML_DOCB_ENABLED
5017 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005018#endif
5019 break;
5020 case XML_NOTATION_NODE:
5021 break;
5022 case XML_DTD_NODE:
5023 break;
5024 case XML_NAMESPACE_DECL:
5025 break;
5026 case XML_ELEMENT_DECL:
5027 /* TODO !!! */
5028 break;
5029 case XML_ATTRIBUTE_DECL:
5030 /* TODO !!! */
5031 break;
5032 case XML_ENTITY_DECL:
5033 /* TODO !!! */
5034 break;
5035 }
5036}
5037
Daniel Veillard652327a2003-09-29 18:02:38 +00005038#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005039/**
5040 * xmlNodeSetContentLen:
5041 * @cur: the node being modified
5042 * @content: the new value of the content
5043 * @len: the size of @content
5044 *
5045 * Replace the content of a node.
5046 */
5047void
5048xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5049 if (cur == NULL) {
5050#ifdef DEBUG_TREE
5051 xmlGenericError(xmlGenericErrorContext,
5052 "xmlNodeSetContentLen : node == NULL\n");
5053#endif
5054 return;
5055 }
5056 switch (cur->type) {
5057 case XML_DOCUMENT_FRAG_NODE:
5058 case XML_ELEMENT_NODE:
Daniel Veillard2c748c62002-01-16 15:37:50 +00005059 case XML_ATTRIBUTE_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005060 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5061 cur->children = xmlStringLenGetNodeList(cur->doc, content, len);
5062 UPDATE_LAST_CHILD_AND_PARENT(cur)
5063 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005064 case XML_TEXT_NODE:
5065 case XML_CDATA_SECTION_NODE:
5066 case XML_ENTITY_REF_NODE:
5067 case XML_ENTITY_NODE:
5068 case XML_PI_NODE:
5069 case XML_COMMENT_NODE:
5070 case XML_NOTATION_NODE:
5071 if (cur->content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005072 xmlFree(cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005073 }
5074 if (cur->children != NULL) xmlFreeNodeList(cur->children);
5075 cur->children = cur->last = NULL;
5076 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005077 cur->content = xmlStrndup(content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005078 } else
5079 cur->content = NULL;
5080 break;
5081 case XML_DOCUMENT_NODE:
5082 case XML_DTD_NODE:
5083 case XML_HTML_DOCUMENT_NODE:
5084 case XML_DOCUMENT_TYPE_NODE:
5085 case XML_NAMESPACE_DECL:
5086 case XML_XINCLUDE_START:
5087 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005088#ifdef LIBXML_DOCB_ENABLED
5089 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005090#endif
5091 break;
5092 case XML_ELEMENT_DECL:
5093 /* TODO !!! */
5094 break;
5095 case XML_ATTRIBUTE_DECL:
5096 /* TODO !!! */
5097 break;
5098 case XML_ENTITY_DECL:
5099 /* TODO !!! */
5100 break;
5101 }
5102}
Daniel Veillard652327a2003-09-29 18:02:38 +00005103#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005104
5105/**
5106 * xmlNodeAddContentLen:
5107 * @cur: the node being modified
5108 * @content: extra content
5109 * @len: the size of @content
5110 *
5111 * Append the extra substring to the node content.
5112 */
5113void
5114xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) {
5115 if (cur == NULL) {
5116#ifdef DEBUG_TREE
5117 xmlGenericError(xmlGenericErrorContext,
5118 "xmlNodeAddContentLen : node == NULL\n");
5119#endif
5120 return;
5121 }
5122 if (len <= 0) return;
5123 switch (cur->type) {
5124 case XML_DOCUMENT_FRAG_NODE:
5125 case XML_ELEMENT_NODE: {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005126 xmlNodePtr last, newNode, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00005127
Daniel Veillard7db37732001-07-12 01:20:08 +00005128 last = cur->last;
Owen Taylor3473f882001-02-23 17:55:21 +00005129 newNode = xmlNewTextLen(content, len);
5130 if (newNode != NULL) {
Daniel Veillardacb2bda2002-01-13 16:15:43 +00005131 tmp = xmlAddChild(cur, newNode);
5132 if (tmp != newNode)
5133 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005134 if ((last != NULL) && (last->next == newNode)) {
5135 xmlTextMerge(last, newNode);
5136 }
5137 }
5138 break;
5139 }
5140 case XML_ATTRIBUTE_NODE:
5141 break;
5142 case XML_TEXT_NODE:
5143 case XML_CDATA_SECTION_NODE:
5144 case XML_ENTITY_REF_NODE:
5145 case XML_ENTITY_NODE:
5146 case XML_PI_NODE:
5147 case XML_COMMENT_NODE:
5148 case XML_NOTATION_NODE:
5149 if (content != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005150 cur->content = xmlStrncat(cur->content, content, len);
Owen Taylor3473f882001-02-23 17:55:21 +00005151 }
5152 case XML_DOCUMENT_NODE:
5153 case XML_DTD_NODE:
5154 case XML_HTML_DOCUMENT_NODE:
5155 case XML_DOCUMENT_TYPE_NODE:
5156 case XML_NAMESPACE_DECL:
5157 case XML_XINCLUDE_START:
5158 case XML_XINCLUDE_END:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005159#ifdef LIBXML_DOCB_ENABLED
5160 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005161#endif
5162 break;
5163 case XML_ELEMENT_DECL:
5164 case XML_ATTRIBUTE_DECL:
5165 case XML_ENTITY_DECL:
5166 break;
5167 }
5168}
5169
5170/**
5171 * xmlNodeAddContent:
5172 * @cur: the node being modified
5173 * @content: extra content
5174 *
5175 * Append the extra substring to the node content.
5176 */
5177void
5178xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) {
5179 int len;
5180
5181 if (cur == NULL) {
5182#ifdef DEBUG_TREE
5183 xmlGenericError(xmlGenericErrorContext,
5184 "xmlNodeAddContent : node == NULL\n");
5185#endif
5186 return;
5187 }
5188 if (content == NULL) return;
5189 len = xmlStrlen(content);
5190 xmlNodeAddContentLen(cur, content, len);
5191}
5192
5193/**
5194 * xmlTextMerge:
5195 * @first: the first text node
5196 * @second: the second text node being merged
5197 *
5198 * Merge two text nodes into one
5199 * Returns the first text node augmented
5200 */
5201xmlNodePtr
5202xmlTextMerge(xmlNodePtr first, xmlNodePtr second) {
5203 if (first == NULL) return(second);
5204 if (second == NULL) return(first);
5205 if (first->type != XML_TEXT_NODE) return(first);
5206 if (second->type != XML_TEXT_NODE) return(first);
5207 if (second->name != first->name)
5208 return(first);
Owen Taylor3473f882001-02-23 17:55:21 +00005209 xmlNodeAddContent(first, second->content);
Owen Taylor3473f882001-02-23 17:55:21 +00005210 xmlUnlinkNode(second);
5211 xmlFreeNode(second);
5212 return(first);
5213}
5214
Daniel Veillard652327a2003-09-29 18:02:38 +00005215#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005216/**
5217 * xmlGetNsList:
5218 * @doc: the document
5219 * @node: the current node
5220 *
5221 * Search all the namespace applying to a given element.
Daniel Veillardd1640922001-12-17 15:30:10 +00005222 * Returns an NULL terminated array of all the #xmlNsPtr found
Owen Taylor3473f882001-02-23 17:55:21 +00005223 * that need to be freed by the caller or NULL if no
5224 * namespace if defined
5225 */
5226xmlNsPtr *
Daniel Veillard77044732001-06-29 21:31:07 +00005227xmlGetNsList(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node)
5228{
Owen Taylor3473f882001-02-23 17:55:21 +00005229 xmlNsPtr cur;
5230 xmlNsPtr *ret = NULL;
5231 int nbns = 0;
5232 int maxns = 10;
5233 int i;
5234
5235 while (node != NULL) {
Daniel Veillard77044732001-06-29 21:31:07 +00005236 if (node->type == XML_ELEMENT_NODE) {
5237 cur = node->nsDef;
5238 while (cur != NULL) {
5239 if (ret == NULL) {
5240 ret =
5241 (xmlNsPtr *) xmlMalloc((maxns + 1) *
5242 sizeof(xmlNsPtr));
5243 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005244 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005245 return (NULL);
5246 }
5247 ret[nbns] = NULL;
5248 }
5249 for (i = 0; i < nbns; i++) {
5250 if ((cur->prefix == ret[i]->prefix) ||
5251 (xmlStrEqual(cur->prefix, ret[i]->prefix)))
5252 break;
5253 }
5254 if (i >= nbns) {
5255 if (nbns >= maxns) {
5256 maxns *= 2;
5257 ret = (xmlNsPtr *) xmlRealloc(ret,
5258 (maxns +
5259 1) *
5260 sizeof(xmlNsPtr));
5261 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005262 xmlTreeErrMemory("getting namespace list");
Daniel Veillard77044732001-06-29 21:31:07 +00005263 return (NULL);
5264 }
5265 }
5266 ret[nbns++] = cur;
5267 ret[nbns] = NULL;
5268 }
Owen Taylor3473f882001-02-23 17:55:21 +00005269
Daniel Veillard77044732001-06-29 21:31:07 +00005270 cur = cur->next;
5271 }
5272 }
5273 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005274 }
Daniel Veillard77044732001-06-29 21:31:07 +00005275 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005276}
Daniel Veillard652327a2003-09-29 18:02:38 +00005277#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005278
5279/**
5280 * xmlSearchNs:
5281 * @doc: the document
5282 * @node: the current node
Daniel Veillard77851712001-02-27 21:54:07 +00005283 * @nameSpace: the namespace prefix
Owen Taylor3473f882001-02-23 17:55:21 +00005284 *
5285 * Search a Ns registered under a given name space for a document.
5286 * recurse on the parents until it finds the defined namespace
5287 * or return NULL otherwise.
5288 * @nameSpace can be NULL, this is a search for the default namespace.
5289 * We don't allow to cross entities boundaries. If you don't declare
5290 * the namespace within those you will be in troubles !!! A warning
5291 * is generated to cover this case.
5292 *
5293 * Returns the namespace pointer or NULL.
5294 */
5295xmlNsPtr
5296xmlSearchNs(xmlDocPtr doc, xmlNodePtr node, const xmlChar *nameSpace) {
Daniel Veillardf4e56292003-10-28 14:27:41 +00005297
Owen Taylor3473f882001-02-23 17:55:21 +00005298 xmlNsPtr cur;
Daniel Veillardf4e56292003-10-28 14:27:41 +00005299 xmlNodePtr orig = node;
Owen Taylor3473f882001-02-23 17:55:21 +00005300
5301 if (node == NULL) return(NULL);
5302 if ((nameSpace != NULL) &&
5303 (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005304 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5305 /*
5306 * The XML-1.0 namespace is normally held on the root
5307 * element. In this case exceptionally create it on the
5308 * node element.
5309 */
5310 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5311 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005312 xmlTreeErrMemory("searching namespace");
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00005313 return(NULL);
5314 }
5315 memset(cur, 0, sizeof(xmlNs));
5316 cur->type = XML_LOCAL_NAMESPACE;
5317 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5318 cur->prefix = xmlStrdup((const xmlChar *)"xml");
5319 cur->next = node->nsDef;
5320 node->nsDef = cur;
5321 return(cur);
5322 }
Owen Taylor3473f882001-02-23 17:55:21 +00005323 if (doc->oldNs == NULL) {
5324 /*
5325 * Allocate a new Namespace and fill the fields.
5326 */
5327 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5328 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005329 xmlTreeErrMemory("searching namespace");
Owen Taylor3473f882001-02-23 17:55:21 +00005330 return(NULL);
5331 }
5332 memset(doc->oldNs, 0, sizeof(xmlNs));
5333 doc->oldNs->type = XML_LOCAL_NAMESPACE;
5334
5335 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5336 doc->oldNs->prefix = xmlStrdup((const xmlChar *)"xml");
5337 }
5338 return(doc->oldNs);
5339 }
5340 while (node != NULL) {
5341 if ((node->type == XML_ENTITY_REF_NODE) ||
5342 (node->type == XML_ENTITY_NODE) ||
5343 (node->type == XML_ENTITY_DECL))
5344 return(NULL);
5345 if (node->type == XML_ELEMENT_NODE) {
5346 cur = node->nsDef;
5347 while (cur != NULL) {
5348 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5349 (cur->href != NULL))
5350 return(cur);
5351 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5352 (cur->href != NULL) &&
5353 (xmlStrEqual(cur->prefix, nameSpace)))
5354 return(cur);
5355 cur = cur->next;
5356 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005357 if (orig != node) {
5358 cur = node->ns;
5359 if (cur != NULL) {
5360 if ((cur->prefix == NULL) && (nameSpace == NULL) &&
5361 (cur->href != NULL))
5362 return(cur);
5363 if ((cur->prefix != NULL) && (nameSpace != NULL) &&
5364 (cur->href != NULL) &&
5365 (xmlStrEqual(cur->prefix, nameSpace)))
5366 return(cur);
5367 }
5368 }
Owen Taylor3473f882001-02-23 17:55:21 +00005369 }
5370 node = node->parent;
5371 }
5372 return(NULL);
5373}
5374
5375/**
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005376 * xmlNsInScope:
5377 * @doc: the document
5378 * @node: the current node
5379 * @ancestor: the ancestor carrying the namespace
5380 * @prefix: the namespace prefix
5381 *
5382 * Verify that the given namespace held on @ancestor is still in scope
5383 * on node.
5384 *
5385 * Returns 1 if true, 0 if false and -1 in case of error.
5386 */
5387static int
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005388xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node,
5389 xmlNodePtr ancestor, const xmlChar * prefix)
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005390{
5391 xmlNsPtr tst;
5392
5393 while ((node != NULL) && (node != ancestor)) {
5394 if ((node->type == XML_ENTITY_REF_NODE) ||
5395 (node->type == XML_ENTITY_NODE) ||
5396 (node->type == XML_ENTITY_DECL))
5397 return (-1);
5398 if (node->type == XML_ELEMENT_NODE) {
5399 tst = node->nsDef;
5400 while (tst != NULL) {
5401 if ((tst->prefix == NULL)
5402 && (prefix == NULL))
5403 return (0);
5404 if ((tst->prefix != NULL)
5405 && (prefix != NULL)
5406 && (xmlStrEqual(tst->prefix, prefix)))
5407 return (0);
5408 tst = tst->next;
5409 }
5410 }
5411 node = node->parent;
5412 }
5413 if (node != ancestor)
5414 return (-1);
5415 return (1);
5416}
5417
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005418/**
Owen Taylor3473f882001-02-23 17:55:21 +00005419 * xmlSearchNsByHref:
5420 * @doc: the document
5421 * @node: the current node
5422 * @href: the namespace value
5423 *
5424 * Search a Ns aliasing a given URI. Recurse on the parents until it finds
5425 * the defined namespace or return NULL otherwise.
5426 * Returns the namespace pointer or NULL.
5427 */
5428xmlNsPtr
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005429xmlSearchNsByHref(xmlDocPtr doc, xmlNodePtr node, const xmlChar * href)
5430{
Owen Taylor3473f882001-02-23 17:55:21 +00005431 xmlNsPtr cur;
5432 xmlNodePtr orig = node;
5433
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005434 if ((node == NULL) || (href == NULL))
5435 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005436 if (xmlStrEqual(href, XML_XML_NAMESPACE)) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005437 /*
5438 * Only the document can hold the XML spec namespace.
5439 */
5440 if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) {
5441 /*
5442 * The XML-1.0 namespace is normally held on the root
5443 * element. In this case exceptionally create it on the
5444 * node element.
5445 */
5446 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5447 if (cur == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005448 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005449 return (NULL);
5450 }
5451 memset(cur, 0, sizeof(xmlNs));
5452 cur->type = XML_LOCAL_NAMESPACE;
5453 cur->href = xmlStrdup(XML_XML_NAMESPACE);
5454 cur->prefix = xmlStrdup((const xmlChar *) "xml");
5455 cur->next = node->nsDef;
5456 node->nsDef = cur;
5457 return (cur);
5458 }
5459 if (doc->oldNs == NULL) {
5460 /*
5461 * Allocate a new Namespace and fill the fields.
5462 */
5463 doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
5464 if (doc->oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005465 xmlTreeErrMemory("searching namespace");
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005466 return (NULL);
5467 }
5468 memset(doc->oldNs, 0, sizeof(xmlNs));
5469 doc->oldNs->type = XML_LOCAL_NAMESPACE;
Owen Taylor3473f882001-02-23 17:55:21 +00005470
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005471 doc->oldNs->href = xmlStrdup(XML_XML_NAMESPACE);
5472 doc->oldNs->prefix = xmlStrdup((const xmlChar *) "xml");
5473 }
5474 return (doc->oldNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005475 }
5476 while (node != NULL) {
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005477 if ((node->type == XML_ENTITY_REF_NODE) ||
5478 (node->type == XML_ENTITY_NODE) ||
5479 (node->type == XML_ENTITY_DECL))
5480 return (NULL);
5481 if (node->type == XML_ELEMENT_NODE) {
5482 cur = node->nsDef;
5483 while (cur != NULL) {
5484 if ((cur->href != NULL) && (href != NULL) &&
5485 (xmlStrEqual(cur->href, href))) {
5486 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5487 return (cur);
5488 }
5489 cur = cur->next;
5490 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005491 if (orig != node) {
5492 cur = node->ns;
5493 if (cur != NULL) {
5494 if ((cur->href != NULL) && (href != NULL) &&
5495 (xmlStrEqual(cur->href, href))) {
5496 if (xmlNsInScope(doc, orig, node, cur->href) == 1)
5497 return (cur);
5498 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005499 }
Daniel Veillardf4e56292003-10-28 14:27:41 +00005500 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005501 }
5502 node = node->parent;
Owen Taylor3473f882001-02-23 17:55:21 +00005503 }
Daniel Veillard2a3fea32003-09-12 09:44:56 +00005504 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005505}
5506
5507/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005508 * xmlNewReconciliedNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005509 * @doc: the document
5510 * @tree: a node expected to hold the new namespace
5511 * @ns: the original namespace
5512 *
5513 * This function tries to locate a namespace definition in a tree
5514 * ancestors, or create a new namespace definition node similar to
5515 * @ns trying to reuse the same prefix. However if the given prefix is
5516 * null (default namespace) or reused within the subtree defined by
5517 * @tree or on one of its ancestors then a new prefix is generated.
5518 * Returns the (new) namespace definition or NULL in case of error
5519 */
5520xmlNsPtr
5521xmlNewReconciliedNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
5522 xmlNsPtr def;
5523 xmlChar prefix[50];
5524 int counter = 1;
5525
5526 if (tree == NULL) {
5527#ifdef DEBUG_TREE
5528 xmlGenericError(xmlGenericErrorContext,
5529 "xmlNewReconciliedNs : tree == NULL\n");
5530#endif
5531 return(NULL);
5532 }
5533 if (ns == NULL) {
5534#ifdef DEBUG_TREE
5535 xmlGenericError(xmlGenericErrorContext,
5536 "xmlNewReconciliedNs : ns == NULL\n");
5537#endif
5538 return(NULL);
5539 }
5540 /*
5541 * Search an existing namespace definition inherited.
5542 */
5543 def = xmlSearchNsByHref(doc, tree, ns->href);
5544 if (def != NULL)
5545 return(def);
5546
5547 /*
5548 * Find a close prefix which is not already in use.
5549 * Let's strip namespace prefixes longer than 20 chars !
5550 */
Daniel Veillardf742d342002-03-07 00:05:35 +00005551 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005552 snprintf((char *) prefix, sizeof(prefix), "default");
Daniel Veillardf742d342002-03-07 00:05:35 +00005553 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005554 snprintf((char *) prefix, sizeof(prefix), "%.20s", ns->prefix);
Daniel Veillardf742d342002-03-07 00:05:35 +00005555
Owen Taylor3473f882001-02-23 17:55:21 +00005556 def = xmlSearchNs(doc, tree, prefix);
5557 while (def != NULL) {
5558 if (counter > 1000) return(NULL);
Daniel Veillardf742d342002-03-07 00:05:35 +00005559 if (ns->prefix == NULL)
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005560 snprintf((char *) prefix, sizeof(prefix), "default%d", counter++);
Daniel Veillardf742d342002-03-07 00:05:35 +00005561 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005562 snprintf((char *) prefix, sizeof(prefix), "%.20s%d", ns->prefix, counter++);
Owen Taylor3473f882001-02-23 17:55:21 +00005563 def = xmlSearchNs(doc, tree, prefix);
5564 }
5565
5566 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005567 * OK, now we are ready to create a new one.
Owen Taylor3473f882001-02-23 17:55:21 +00005568 */
5569 def = xmlNewNs(tree, ns->href, prefix);
5570 return(def);
5571}
5572
Daniel Veillard652327a2003-09-29 18:02:38 +00005573#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00005574/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00005575 * xmlReconciliateNs:
Owen Taylor3473f882001-02-23 17:55:21 +00005576 * @doc: the document
5577 * @tree: a node defining the subtree to reconciliate
5578 *
5579 * This function checks that all the namespaces declared within the given
5580 * tree are properly declared. This is needed for example after Copy or Cut
5581 * and then paste operations. The subtree may still hold pointers to
5582 * namespace declarations outside the subtree or invalid/masked. As much
Daniel Veillardd1640922001-12-17 15:30:10 +00005583 * as possible the function try to reuse the existing namespaces found in
Owen Taylor3473f882001-02-23 17:55:21 +00005584 * the new environment. If not possible the new namespaces are redeclared
5585 * on @tree at the top of the given subtree.
5586 * Returns the number of namespace declarations created or -1 in case of error.
5587 */
5588int
5589xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) {
5590 xmlNsPtr *oldNs = NULL;
5591 xmlNsPtr *newNs = NULL;
5592 int sizeCache = 0;
5593 int nbCache = 0;
5594
5595 xmlNsPtr n;
5596 xmlNodePtr node = tree;
5597 xmlAttrPtr attr;
5598 int ret = 0, i;
5599
5600 while (node != NULL) {
5601 /*
5602 * Reconciliate the node namespace
5603 */
5604 if (node->ns != NULL) {
5605 /*
5606 * initialize the cache if needed
5607 */
5608 if (sizeCache == 0) {
5609 sizeCache = 10;
5610 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5611 sizeof(xmlNsPtr));
5612 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005613 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005614 return(-1);
5615 }
5616 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5617 sizeof(xmlNsPtr));
5618 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005619 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005620 xmlFree(oldNs);
5621 return(-1);
5622 }
5623 }
5624 for (i = 0;i < nbCache;i++) {
5625 if (oldNs[i] == node->ns) {
5626 node->ns = newNs[i];
5627 break;
5628 }
5629 }
5630 if (i == nbCache) {
5631 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005632 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005633 */
5634 n = xmlNewReconciliedNs(doc, tree, node->ns);
5635 if (n != NULL) { /* :-( what if else ??? */
5636 /*
5637 * check if we need to grow the cache buffers.
5638 */
5639 if (sizeCache <= nbCache) {
5640 sizeCache *= 2;
5641 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5642 sizeof(xmlNsPtr));
5643 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005644 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005645 xmlFree(newNs);
5646 return(-1);
5647 }
5648 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5649 sizeof(xmlNsPtr));
5650 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005651 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005652 xmlFree(oldNs);
5653 return(-1);
5654 }
5655 }
5656 newNs[nbCache] = n;
5657 oldNs[nbCache++] = node->ns;
5658 node->ns = n;
5659 }
5660 }
5661 }
5662 /*
5663 * now check for namespace hold by attributes on the node.
5664 */
5665 attr = node->properties;
5666 while (attr != NULL) {
5667 if (attr->ns != NULL) {
5668 /*
5669 * initialize the cache if needed
5670 */
5671 if (sizeCache == 0) {
5672 sizeCache = 10;
5673 oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5674 sizeof(xmlNsPtr));
5675 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005676 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005677 return(-1);
5678 }
5679 newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
5680 sizeof(xmlNsPtr));
5681 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005682 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005683 xmlFree(oldNs);
5684 return(-1);
5685 }
5686 }
5687 for (i = 0;i < nbCache;i++) {
5688 if (oldNs[i] == attr->ns) {
Daniel Veillardce66ce12002-10-28 19:01:59 +00005689 attr->ns = newNs[i];
Owen Taylor3473f882001-02-23 17:55:21 +00005690 break;
5691 }
5692 }
5693 if (i == nbCache) {
5694 /*
Daniel Veillardd1640922001-12-17 15:30:10 +00005695 * OK we need to recreate a new namespace definition
Owen Taylor3473f882001-02-23 17:55:21 +00005696 */
5697 n = xmlNewReconciliedNs(doc, tree, attr->ns);
5698 if (n != NULL) { /* :-( what if else ??? */
5699 /*
5700 * check if we need to grow the cache buffers.
5701 */
5702 if (sizeCache <= nbCache) {
5703 sizeCache *= 2;
5704 oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
5705 sizeof(xmlNsPtr));
5706 if (oldNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005707 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005708 xmlFree(newNs);
5709 return(-1);
5710 }
5711 newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
5712 sizeof(xmlNsPtr));
5713 if (newNs == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00005714 xmlTreeErrMemory("fixing namespaces");
Owen Taylor3473f882001-02-23 17:55:21 +00005715 xmlFree(oldNs);
5716 return(-1);
5717 }
5718 }
5719 newNs[nbCache] = n;
5720 oldNs[nbCache++] = attr->ns;
5721 attr->ns = n;
5722 }
5723 }
5724 }
5725 attr = attr->next;
5726 }
5727
5728 /*
5729 * Browse the full subtree, deep first
5730 */
5731 if (node->children != NULL) {
5732 /* deep first */
5733 node = node->children;
5734 } else if ((node != tree) && (node->next != NULL)) {
5735 /* then siblings */
5736 node = node->next;
5737 } else if (node != tree) {
5738 /* go up to parents->next if needed */
5739 while (node != tree) {
5740 if (node->parent != NULL)
5741 node = node->parent;
5742 if ((node != tree) && (node->next != NULL)) {
5743 node = node->next;
5744 break;
5745 }
5746 if (node->parent == NULL) {
5747 node = NULL;
5748 break;
5749 }
5750 }
5751 /* exit condition */
5752 if (node == tree)
5753 node = NULL;
Daniel Veillard1e774382002-03-06 17:35:40 +00005754 } else
5755 break;
Owen Taylor3473f882001-02-23 17:55:21 +00005756 }
Daniel Veillardf742d342002-03-07 00:05:35 +00005757 if (oldNs != NULL)
5758 xmlFree(oldNs);
5759 if (newNs != NULL)
5760 xmlFree(newNs);
Owen Taylor3473f882001-02-23 17:55:21 +00005761 return(ret);
5762}
Daniel Veillard652327a2003-09-29 18:02:38 +00005763#endif /* LIBXML_TREE_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00005764
5765/**
5766 * xmlHasProp:
5767 * @node: the node
5768 * @name: the attribute name
5769 *
5770 * Search an attribute associated to a node
5771 * This function also looks in DTD attribute declaration for #FIXED or
5772 * default declaration values unless DTD use has been turned off.
5773 *
5774 * Returns the attribute or the attribute declaration or NULL if
5775 * neither was found.
5776 */
5777xmlAttrPtr
5778xmlHasProp(xmlNodePtr node, const xmlChar *name) {
5779 xmlAttrPtr prop;
5780 xmlDocPtr doc;
5781
5782 if ((node == NULL) || (name == NULL)) return(NULL);
5783 /*
5784 * Check on the properties attached to the node
5785 */
5786 prop = node->properties;
5787 while (prop != NULL) {
5788 if (xmlStrEqual(prop->name, name)) {
5789 return(prop);
5790 }
5791 prop = prop->next;
5792 }
5793 if (!xmlCheckDTD) return(NULL);
5794
5795 /*
5796 * Check if there is a default declaration in the internal
5797 * or external subsets
5798 */
5799 doc = node->doc;
5800 if (doc != NULL) {
5801 xmlAttributePtr attrDecl;
5802 if (doc->intSubset != NULL) {
5803 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5804 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5805 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005806 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5807 /* return attribute declaration only if a default value is given
5808 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005809 return((xmlAttrPtr) attrDecl);
5810 }
5811 }
5812 return(NULL);
5813}
5814
5815/**
Daniel Veillarde95e2392001-06-06 10:46:28 +00005816 * xmlHasNsProp:
5817 * @node: the node
5818 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00005819 * @nameSpace: the URI of the namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005820 *
5821 * Search for an attribute associated to a node
5822 * This attribute has to be anchored in the namespace specified.
5823 * This does the entity substitution.
5824 * This function looks in DTD attribute declaration for #FIXED or
5825 * default declaration values unless DTD use has been turned off.
5826 *
5827 * Returns the attribute or the attribute declaration or NULL
5828 * if neither was found.
5829 */
5830xmlAttrPtr
Daniel Veillardca2366a2001-06-11 12:09:01 +00005831xmlHasNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Daniel Veillarde95e2392001-06-06 10:46:28 +00005832 xmlAttrPtr prop;
Daniel Veillard652327a2003-09-29 18:02:38 +00005833#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005834 xmlDocPtr doc;
Daniel Veillard652327a2003-09-29 18:02:38 +00005835#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005836
5837 if (node == NULL)
5838 return(NULL);
5839
5840 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00005841 if (nameSpace == NULL)
Daniel Veillarde95e2392001-06-06 10:46:28 +00005842 return(xmlHasProp(node, name));
5843 while (prop != NULL) {
5844 /*
5845 * One need to have
5846 * - same attribute names
5847 * - and the attribute carrying that namespace
Daniel Veillarde95e2392001-06-06 10:46:28 +00005848 */
5849 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde3c81b52001-06-17 14:50:34 +00005850 ((prop->ns != NULL) && (xmlStrEqual(prop->ns->href, nameSpace)))) {
5851 return(prop);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005852 }
5853 prop = prop->next;
5854 }
5855 if (!xmlCheckDTD) return(NULL);
5856
Daniel Veillard652327a2003-09-29 18:02:38 +00005857#ifdef LIBXML_TREE_ENABLED
Daniel Veillarde95e2392001-06-06 10:46:28 +00005858 /*
5859 * Check if there is a default declaration in the internal
5860 * or external subsets
5861 */
5862 doc = node->doc;
5863 if (doc != NULL) {
5864 if (doc->intSubset != NULL) {
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005865 xmlAttributePtr attrDecl = NULL;
5866 xmlNsPtr *nsList, *cur;
5867 xmlChar *ename;
Daniel Veillarde95e2392001-06-06 10:46:28 +00005868
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005869 nsList = xmlGetNsList(node->doc, node);
5870 if (nsList == NULL)
5871 return(NULL);
5872 if ((node->ns != NULL) && (node->ns->prefix != NULL)) {
5873 ename = xmlStrdup(node->ns->prefix);
5874 ename = xmlStrcat(ename, BAD_CAST ":");
5875 ename = xmlStrcat(ename, node->name);
5876 } else {
5877 ename = xmlStrdup(node->name);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005878 }
Daniel Veillardef6c46f2002-03-07 22:21:56 +00005879 if (ename == NULL) {
5880 xmlFree(nsList);
5881 return(NULL);
5882 }
5883
5884 cur = nsList;
5885 while (*cur != NULL) {
5886 if (xmlStrEqual((*cur)->href, nameSpace)) {
5887 attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, ename,
5888 name, (*cur)->prefix);
5889 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5890 attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, ename,
5891 name, (*cur)->prefix);
5892 }
5893 cur++;
5894 }
5895 xmlFree(nsList);
5896 xmlFree(ename);
5897 return((xmlAttrPtr) attrDecl);
Daniel Veillarde95e2392001-06-06 10:46:28 +00005898 }
5899 }
Daniel Veillard652327a2003-09-29 18:02:38 +00005900#endif /* LIBXML_TREE_ENABLED */
Daniel Veillarde95e2392001-06-06 10:46:28 +00005901 return(NULL);
5902}
5903
5904/**
Owen Taylor3473f882001-02-23 17:55:21 +00005905 * xmlGetProp:
5906 * @node: the node
5907 * @name: the attribute name
5908 *
5909 * Search and get the value of an attribute associated to a node
5910 * This does the entity substitution.
5911 * This function looks in DTD attribute declaration for #FIXED or
5912 * default declaration values unless DTD use has been turned off.
Daniel Veillard784b9352003-02-16 15:50:27 +00005913 * NOTE: this function acts independently of namespaces associated
Daniel Veillard71531f32003-02-05 13:19:53 +00005914 * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp()
5915 * for namespace aware processing.
Owen Taylor3473f882001-02-23 17:55:21 +00005916 *
5917 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00005918 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00005919 */
5920xmlChar *
5921xmlGetProp(xmlNodePtr node, const xmlChar *name) {
5922 xmlAttrPtr prop;
5923 xmlDocPtr doc;
5924
5925 if ((node == NULL) || (name == NULL)) return(NULL);
5926 /*
5927 * Check on the properties attached to the node
5928 */
5929 prop = node->properties;
5930 while (prop != NULL) {
5931 if (xmlStrEqual(prop->name, name)) {
5932 xmlChar *ret;
5933
5934 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5935 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5936 return(ret);
5937 }
5938 prop = prop->next;
5939 }
5940 if (!xmlCheckDTD) return(NULL);
5941
5942 /*
5943 * Check if there is a default declaration in the internal
5944 * or external subsets
5945 */
5946 doc = node->doc;
5947 if (doc != NULL) {
5948 xmlAttributePtr attrDecl;
5949 if (doc->intSubset != NULL) {
5950 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
5951 if ((attrDecl == NULL) && (doc->extSubset != NULL))
5952 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00005953 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
5954 /* return attribute declaration only if a default value is given
5955 (that includes #FIXED declarations) */
Owen Taylor3473f882001-02-23 17:55:21 +00005956 return(xmlStrdup(attrDecl->defaultValue));
5957 }
5958 }
5959 return(NULL);
5960}
5961
5962/**
Daniel Veillard71531f32003-02-05 13:19:53 +00005963 * xmlGetNoNsProp:
5964 * @node: the node
5965 * @name: the attribute name
5966 *
5967 * Search and get the value of an attribute associated to a node
5968 * This does the entity substitution.
5969 * This function looks in DTD attribute declaration for #FIXED or
5970 * default declaration values unless DTD use has been turned off.
5971 * This function is similar to xmlGetProp except it will accept only
5972 * an attribute in no namespace.
5973 *
5974 * Returns the attribute value or NULL if not found.
5975 * It's up to the caller to free the memory with xmlFree().
5976 */
5977xmlChar *
5978xmlGetNoNsProp(xmlNodePtr node, const xmlChar *name) {
5979 xmlAttrPtr prop;
5980 xmlDocPtr doc;
5981
5982 if ((node == NULL) || (name == NULL)) return(NULL);
5983 /*
5984 * Check on the properties attached to the node
5985 */
5986 prop = node->properties;
5987 while (prop != NULL) {
5988 if ((prop->ns == NULL) && (xmlStrEqual(prop->name, name))) {
5989 xmlChar *ret;
5990
5991 ret = xmlNodeListGetString(node->doc, prop->children, 1);
5992 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
5993 return(ret);
5994 }
5995 prop = prop->next;
5996 }
5997 if (!xmlCheckDTD) return(NULL);
5998
5999 /*
6000 * Check if there is a default declaration in the internal
6001 * or external subsets
6002 */
6003 doc = node->doc;
6004 if (doc != NULL) {
6005 xmlAttributePtr attrDecl;
6006 if (doc->intSubset != NULL) {
6007 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6008 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6009 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
Daniel Veillard75eb1ad2003-07-07 14:42:44 +00006010 if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL))
6011 /* return attribute declaration only if a default value is given
6012 (that includes #FIXED declarations) */
Daniel Veillard71531f32003-02-05 13:19:53 +00006013 return(xmlStrdup(attrDecl->defaultValue));
6014 }
6015 }
6016 return(NULL);
6017}
6018
6019/**
Owen Taylor3473f882001-02-23 17:55:21 +00006020 * xmlGetNsProp:
6021 * @node: the node
6022 * @name: the attribute name
Daniel Veillardca2366a2001-06-11 12:09:01 +00006023 * @nameSpace: the URI of the namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006024 *
6025 * Search and get the value of an attribute associated to a node
6026 * This attribute has to be anchored in the namespace specified.
6027 * This does the entity substitution.
6028 * This function looks in DTD attribute declaration for #FIXED or
6029 * default declaration values unless DTD use has been turned off.
6030 *
6031 * Returns the attribute value or NULL if not found.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006032 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00006033 */
6034xmlChar *
Daniel Veillardca2366a2001-06-11 12:09:01 +00006035xmlGetNsProp(xmlNodePtr node, const xmlChar *name, const xmlChar *nameSpace) {
Owen Taylor3473f882001-02-23 17:55:21 +00006036 xmlAttrPtr prop;
6037 xmlDocPtr doc;
6038 xmlNsPtr ns;
6039
6040 if (node == NULL)
6041 return(NULL);
6042
6043 prop = node->properties;
Daniel Veillardca2366a2001-06-11 12:09:01 +00006044 if (nameSpace == NULL)
Daniel Veillard71531f32003-02-05 13:19:53 +00006045 return(xmlGetNoNsProp(node, name));
Owen Taylor3473f882001-02-23 17:55:21 +00006046 while (prop != NULL) {
6047 /*
6048 * One need to have
6049 * - same attribute names
6050 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006051 */
6052 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarde8fc08e2001-06-07 19:35:47 +00006053 ((prop->ns != NULL) &&
Daniel Veillardca2366a2001-06-11 12:09:01 +00006054 (xmlStrEqual(prop->ns->href, nameSpace)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006055 xmlChar *ret;
6056
6057 ret = xmlNodeListGetString(node->doc, prop->children, 1);
6058 if (ret == NULL) return(xmlStrdup((xmlChar *)""));
6059 return(ret);
6060 }
6061 prop = prop->next;
6062 }
6063 if (!xmlCheckDTD) return(NULL);
6064
6065 /*
6066 * Check if there is a default declaration in the internal
6067 * or external subsets
6068 */
6069 doc = node->doc;
6070 if (doc != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00006071 if (doc->intSubset != NULL) {
Daniel Veillard5792e162001-04-30 17:44:45 +00006072 xmlAttributePtr attrDecl;
6073
Owen Taylor3473f882001-02-23 17:55:21 +00006074 attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name);
6075 if ((attrDecl == NULL) && (doc->extSubset != NULL))
6076 attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name);
6077
6078 if ((attrDecl != NULL) && (attrDecl->prefix != NULL)) {
6079 /*
6080 * The DTD declaration only allows a prefix search
6081 */
6082 ns = xmlSearchNs(doc, node, attrDecl->prefix);
Daniel Veillardca2366a2001-06-11 12:09:01 +00006083 if ((ns != NULL) && (xmlStrEqual(ns->href, nameSpace)))
Owen Taylor3473f882001-02-23 17:55:21 +00006084 return(xmlStrdup(attrDecl->defaultValue));
6085 }
6086 }
6087 }
6088 return(NULL);
6089}
6090
Daniel Veillard652327a2003-09-29 18:02:38 +00006091#ifdef LIBXML_TREE_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006092/**
6093 * xmlSetProp:
6094 * @node: the node
6095 * @name: the attribute name
6096 * @value: the attribute value
6097 *
6098 * Set (or reset) an attribute carried by a node.
6099 * Returns the attribute pointer.
6100 */
6101xmlAttrPtr
6102xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006103 xmlAttrPtr prop;
6104 xmlDocPtr doc;
Owen Taylor3473f882001-02-23 17:55:21 +00006105
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006106 if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE))
Owen Taylor3473f882001-02-23 17:55:21 +00006107 return(NULL);
6108 doc = node->doc;
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006109 prop = node->properties;
Owen Taylor3473f882001-02-23 17:55:21 +00006110 while (prop != NULL) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006111 if ((xmlStrEqual(prop->name, name)) &&
6112 (prop->ns == NULL)){
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006113 xmlNodePtr oldprop = prop->children;
6114
Owen Taylor3473f882001-02-23 17:55:21 +00006115 prop->children = NULL;
6116 prop->last = NULL;
6117 if (value != NULL) {
6118 xmlChar *buffer;
6119 xmlNodePtr tmp;
6120
6121 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6122 prop->children = xmlStringGetNodeList(node->doc, buffer);
6123 prop->last = NULL;
6124 prop->doc = doc;
6125 tmp = prop->children;
6126 while (tmp != NULL) {
6127 tmp->parent = (xmlNodePtr) prop;
6128 tmp->doc = doc;
6129 if (tmp->next == NULL)
6130 prop->last = tmp;
6131 tmp = tmp->next;
6132 }
6133 xmlFree(buffer);
Daniel Veillard75bea542001-05-11 17:41:21 +00006134 }
Daniel Veillard4855c8c2001-11-25 10:35:25 +00006135 if (oldprop != NULL)
6136 xmlFreeNodeList(oldprop);
Owen Taylor3473f882001-02-23 17:55:21 +00006137 return(prop);
6138 }
6139 prop = prop->next;
6140 }
6141 prop = xmlNewProp(node, name, value);
6142 return(prop);
6143}
6144
6145/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006146 * xmlUnsetProp:
6147 * @node: the node
6148 * @name: the attribute name
6149 *
6150 * Remove an attribute carried by a node.
6151 * Returns 0 if successful, -1 if not found
6152 */
6153int
6154xmlUnsetProp(xmlNodePtr node, const xmlChar *name) {
Daniel Veillard814a76d2003-01-23 18:24:20 +00006155 xmlAttrPtr prop, prev = NULL;;
Daniel Veillard75bea542001-05-11 17:41:21 +00006156
6157 if ((node == NULL) || (name == NULL))
6158 return(-1);
Daniel Veillard814a76d2003-01-23 18:24:20 +00006159 prop = node->properties;
Daniel Veillard75bea542001-05-11 17:41:21 +00006160 while (prop != NULL) {
6161 if ((xmlStrEqual(prop->name, name)) &&
6162 (prop->ns == NULL)) {
6163 if (prev == NULL)
6164 node->properties = prop->next;
6165 else
6166 prev->next = prop->next;
6167 xmlFreeProp(prop);
6168 return(0);
6169 }
6170 prev = prop;
6171 prop = prop->next;
6172 }
6173 return(-1);
6174}
6175
6176/**
Owen Taylor3473f882001-02-23 17:55:21 +00006177 * xmlSetNsProp:
6178 * @node: the node
6179 * @ns: the namespace definition
6180 * @name: the attribute name
6181 * @value: the attribute value
6182 *
6183 * Set (or reset) an attribute carried by a node.
6184 * The ns structure must be in scope, this is not checked.
6185 *
6186 * Returns the attribute pointer.
6187 */
6188xmlAttrPtr
6189xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
6190 const xmlChar *value) {
6191 xmlAttrPtr prop;
6192
6193 if ((node == NULL) || (name == NULL))
6194 return(NULL);
6195
6196 if (ns == NULL)
6197 return(xmlSetProp(node, name, value));
6198 if (ns->href == NULL)
6199 return(NULL);
6200 prop = node->properties;
6201
6202 while (prop != NULL) {
6203 /*
6204 * One need to have
6205 * - same attribute names
6206 * - and the attribute carrying that namespace
Owen Taylor3473f882001-02-23 17:55:21 +00006207 */
6208 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillarda57c26e2002-08-01 12:52:24 +00006209 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006210 if (prop->children != NULL)
6211 xmlFreeNodeList(prop->children);
6212 prop->children = NULL;
6213 prop->last = NULL;
6214 prop->ns = ns;
6215 if (value != NULL) {
6216 xmlChar *buffer;
6217 xmlNodePtr tmp;
6218
6219 buffer = xmlEncodeEntitiesReentrant(node->doc, value);
6220 prop->children = xmlStringGetNodeList(node->doc, buffer);
6221 prop->last = NULL;
6222 tmp = prop->children;
6223 while (tmp != NULL) {
6224 tmp->parent = (xmlNodePtr) prop;
6225 if (tmp->next == NULL)
6226 prop->last = tmp;
6227 tmp = tmp->next;
6228 }
6229 xmlFree(buffer);
6230 }
6231 return(prop);
6232 }
6233 prop = prop->next;
6234 }
6235 prop = xmlNewNsProp(node, ns, name, value);
6236 return(prop);
6237}
6238
6239/**
Daniel Veillard75bea542001-05-11 17:41:21 +00006240 * xmlUnsetNsProp:
6241 * @node: the node
6242 * @ns: the namespace definition
6243 * @name: the attribute name
6244 *
6245 * Remove an attribute carried by a node.
6246 * Returns 0 if successful, -1 if not found
6247 */
6248int
6249xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) {
6250 xmlAttrPtr prop = node->properties, prev = NULL;;
6251
6252 if ((node == NULL) || (name == NULL))
6253 return(-1);
6254 if (ns == NULL)
6255 return(xmlUnsetProp(node, name));
6256 if (ns->href == NULL)
6257 return(-1);
6258 while (prop != NULL) {
6259 if ((xmlStrEqual(prop->name, name)) &&
Daniel Veillard0bf29002002-08-01 12:54:11 +00006260 (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
Daniel Veillard75bea542001-05-11 17:41:21 +00006261 if (prev == NULL)
6262 node->properties = prop->next;
6263 else
6264 prev->next = prop->next;
6265 xmlFreeProp(prop);
6266 return(0);
6267 }
6268 prev = prop;
6269 prop = prop->next;
6270 }
6271 return(-1);
6272}
Daniel Veillard652327a2003-09-29 18:02:38 +00006273#endif /* LIBXML_TREE_ENABLED */
Daniel Veillard75bea542001-05-11 17:41:21 +00006274
6275/**
Owen Taylor3473f882001-02-23 17:55:21 +00006276 * xmlNodeIsText:
6277 * @node: the node
6278 *
6279 * Is this node a Text node ?
6280 * Returns 1 yes, 0 no
6281 */
6282int
6283xmlNodeIsText(xmlNodePtr node) {
6284 if (node == NULL) return(0);
6285
6286 if (node->type == XML_TEXT_NODE) return(1);
6287 return(0);
6288}
6289
6290/**
6291 * xmlIsBlankNode:
6292 * @node: the node
6293 *
6294 * Checks whether this node is an empty or whitespace only
6295 * (and possibly ignorable) text-node.
6296 *
6297 * Returns 1 yes, 0 no
6298 */
6299int
6300xmlIsBlankNode(xmlNodePtr node) {
6301 const xmlChar *cur;
6302 if (node == NULL) return(0);
6303
Daniel Veillard7db37732001-07-12 01:20:08 +00006304 if ((node->type != XML_TEXT_NODE) &&
6305 (node->type != XML_CDATA_SECTION_NODE))
6306 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006307 if (node->content == NULL) return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00006308 cur = node->content;
Owen Taylor3473f882001-02-23 17:55:21 +00006309 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006310 if (!IS_BLANK_CH(*cur)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006311 cur++;
6312 }
6313
6314 return(1);
6315}
6316
6317/**
6318 * xmlTextConcat:
6319 * @node: the node
6320 * @content: the content
Daniel Veillard60087f32001-10-10 09:45:09 +00006321 * @len: @content length
Owen Taylor3473f882001-02-23 17:55:21 +00006322 *
6323 * Concat the given string at the end of the existing node content
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006324 *
6325 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00006326 */
6327
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006328int
Owen Taylor3473f882001-02-23 17:55:21 +00006329xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006330 if (node == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006331
6332 if ((node->type != XML_TEXT_NODE) &&
6333 (node->type != XML_CDATA_SECTION_NODE)) {
6334#ifdef DEBUG_TREE
6335 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006336 "xmlTextConcat: node is not text nor CDATA\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006337#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006338 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00006339 }
Owen Taylor3473f882001-02-23 17:55:21 +00006340 node->content = xmlStrncat(node->content, content, len);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006341 if (node->content == NULL)
6342 return(-1);
6343 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006344}
6345
6346/************************************************************************
6347 * *
6348 * Output : to a FILE or in memory *
6349 * *
6350 ************************************************************************/
6351
Owen Taylor3473f882001-02-23 17:55:21 +00006352/**
6353 * xmlBufferCreate:
6354 *
6355 * routine to create an XML buffer.
6356 * returns the new structure.
6357 */
6358xmlBufferPtr
6359xmlBufferCreate(void) {
6360 xmlBufferPtr ret;
6361
6362 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6363 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006364 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006365 return(NULL);
6366 }
6367 ret->use = 0;
Daniel Veillarde356c282001-03-10 12:32:04 +00006368 ret->size = xmlDefaultBufferSize;
Owen Taylor3473f882001-02-23 17:55:21 +00006369 ret->alloc = xmlBufferAllocScheme;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006370 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006371 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006372 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006373 xmlFree(ret);
6374 return(NULL);
6375 }
6376 ret->content[0] = 0;
6377 return(ret);
6378}
6379
6380/**
6381 * xmlBufferCreateSize:
6382 * @size: initial size of buffer
6383 *
6384 * routine to create an XML buffer.
6385 * returns the new structure.
6386 */
6387xmlBufferPtr
6388xmlBufferCreateSize(size_t size) {
6389 xmlBufferPtr ret;
6390
6391 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6392 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006393 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006394 return(NULL);
6395 }
6396 ret->use = 0;
6397 ret->alloc = xmlBufferAllocScheme;
6398 ret->size = (size ? size+2 : 0); /* +1 for ending null */
6399 if (ret->size){
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006400 ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00006401 if (ret->content == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006402 xmlTreeErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006403 xmlFree(ret);
6404 return(NULL);
6405 }
6406 ret->content[0] = 0;
6407 } else
6408 ret->content = NULL;
6409 return(ret);
6410}
6411
6412/**
Daniel Veillard53350552003-09-18 13:35:51 +00006413 * xmlBufferCreateStatic:
6414 * @mem: the memory area
6415 * @size: the size in byte
6416 *
6417 * routine to create an XML buffer from an immutable memory area,
6418 * The are won't be modified nor copied, and is expected to be
6419 * present until the end of the buffer lifetime.
6420 *
6421 * returns the new structure.
6422 */
6423xmlBufferPtr
6424xmlBufferCreateStatic(void *mem, size_t size) {
6425 xmlBufferPtr ret;
6426
6427 if ((mem == NULL) || (size == 0))
6428 return(NULL);
6429
6430 ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
6431 if (ret == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006432 xmlTreeErrMemory("creating buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00006433 return(NULL);
6434 }
6435 ret->use = size;
6436 ret->size = size;
6437 ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE;
6438 ret->content = (xmlChar *) mem;
6439 return(ret);
6440}
6441
6442/**
Owen Taylor3473f882001-02-23 17:55:21 +00006443 * xmlBufferSetAllocationScheme:
Daniel Veillardbd9afb52002-09-25 22:25:35 +00006444 * @buf: the buffer to tune
Owen Taylor3473f882001-02-23 17:55:21 +00006445 * @scheme: allocation scheme to use
6446 *
6447 * Sets the allocation scheme for this buffer
6448 */
6449void
6450xmlBufferSetAllocationScheme(xmlBufferPtr buf,
6451 xmlBufferAllocationScheme scheme) {
6452 if (buf == NULL) {
6453#ifdef DEBUG_BUFFER
6454 xmlGenericError(xmlGenericErrorContext,
6455 "xmlBufferSetAllocationScheme: buf == NULL\n");
6456#endif
6457 return;
6458 }
Daniel Veillard53350552003-09-18 13:35:51 +00006459 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006460
6461 buf->alloc = scheme;
6462}
6463
6464/**
6465 * xmlBufferFree:
6466 * @buf: the buffer to free
6467 *
Daniel Veillard9d06d302002-01-22 18:15:52 +00006468 * Frees an XML buffer. It frees both the content and the structure which
6469 * encapsulate it.
Owen Taylor3473f882001-02-23 17:55:21 +00006470 */
6471void
6472xmlBufferFree(xmlBufferPtr buf) {
6473 if (buf == NULL) {
6474#ifdef DEBUG_BUFFER
6475 xmlGenericError(xmlGenericErrorContext,
6476 "xmlBufferFree: buf == NULL\n");
6477#endif
6478 return;
6479 }
Daniel Veillard53350552003-09-18 13:35:51 +00006480
6481 if ((buf->content != NULL) &&
6482 (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006483 xmlFree(buf->content);
6484 }
Owen Taylor3473f882001-02-23 17:55:21 +00006485 xmlFree(buf);
6486}
6487
6488/**
6489 * xmlBufferEmpty:
6490 * @buf: the buffer
6491 *
6492 * empty a buffer.
6493 */
6494void
6495xmlBufferEmpty(xmlBufferPtr buf) {
6496 if (buf->content == NULL) return;
6497 buf->use = 0;
Daniel Veillard53350552003-09-18 13:35:51 +00006498 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +00006499 buf->content = BAD_CAST "";
Daniel Veillard53350552003-09-18 13:35:51 +00006500 } else {
6501 memset(buf->content, 0, buf->size);
6502 }
Owen Taylor3473f882001-02-23 17:55:21 +00006503}
6504
6505/**
6506 * xmlBufferShrink:
6507 * @buf: the buffer to dump
6508 * @len: the number of xmlChar to remove
6509 *
6510 * Remove the beginning of an XML buffer.
6511 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006512 * Returns the number of #xmlChar removed, or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00006513 */
6514int
6515xmlBufferShrink(xmlBufferPtr buf, unsigned int len) {
6516 if (len == 0) return(0);
6517 if (len > buf->use) return(-1);
6518
6519 buf->use -= len;
Daniel Veillard53350552003-09-18 13:35:51 +00006520 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) {
6521 buf->content += len;
6522 } else {
6523 memmove(buf->content, &buf->content[len], buf->use * sizeof(xmlChar));
6524 buf->content[buf->use] = 0;
6525 }
Owen Taylor3473f882001-02-23 17:55:21 +00006526 return(len);
6527}
6528
6529/**
6530 * xmlBufferGrow:
6531 * @buf: the buffer
6532 * @len: the minimum free size to allocate
6533 *
6534 * Grow the available space of an XML buffer.
6535 *
6536 * Returns the new available space or -1 in case of error
6537 */
6538int
6539xmlBufferGrow(xmlBufferPtr buf, unsigned int len) {
6540 int size;
6541 xmlChar *newbuf;
6542
Daniel Veillard53350552003-09-18 13:35:51 +00006543 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00006544 if (len + buf->use < buf->size) return(0);
6545
6546 size = buf->use + len + 100;
6547
6548 newbuf = (xmlChar *) xmlRealloc(buf->content, size);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006549 if (newbuf == NULL) {
6550 xmlTreeErrMemory("growing buffer");
6551 return(-1);
6552 }
Owen Taylor3473f882001-02-23 17:55:21 +00006553 buf->content = newbuf;
6554 buf->size = size;
6555 return(buf->size - buf->use);
6556}
6557
6558/**
6559 * xmlBufferDump:
6560 * @file: the file output
6561 * @buf: the buffer to dump
6562 *
6563 * Dumps an XML buffer to a FILE *.
Daniel Veillardd1640922001-12-17 15:30:10 +00006564 * Returns the number of #xmlChar written
Owen Taylor3473f882001-02-23 17:55:21 +00006565 */
6566int
6567xmlBufferDump(FILE *file, xmlBufferPtr buf) {
6568 int ret;
6569
6570 if (buf == NULL) {
6571#ifdef DEBUG_BUFFER
6572 xmlGenericError(xmlGenericErrorContext,
6573 "xmlBufferDump: buf == NULL\n");
6574#endif
6575 return(0);
6576 }
6577 if (buf->content == NULL) {
6578#ifdef DEBUG_BUFFER
6579 xmlGenericError(xmlGenericErrorContext,
6580 "xmlBufferDump: buf->content == NULL\n");
6581#endif
6582 return(0);
6583 }
Daniel Veillardcd337f02001-11-22 18:20:37 +00006584 if (file == NULL)
6585 file = stdout;
Owen Taylor3473f882001-02-23 17:55:21 +00006586 ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file);
6587 return(ret);
6588}
6589
6590/**
6591 * xmlBufferContent:
6592 * @buf: the buffer
6593 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006594 * Function to extract the content of a buffer
6595 *
Owen Taylor3473f882001-02-23 17:55:21 +00006596 * Returns the internal content
6597 */
6598
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006599const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006600xmlBufferContent(const xmlBufferPtr buf)
6601{
6602 if(!buf)
6603 return NULL;
6604
6605 return buf->content;
6606}
6607
6608/**
6609 * xmlBufferLength:
6610 * @buf: the buffer
6611 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00006612 * Function to get the length of a buffer
6613 *
Owen Taylor3473f882001-02-23 17:55:21 +00006614 * Returns the length of data in the internal content
6615 */
6616
6617int
6618xmlBufferLength(const xmlBufferPtr buf)
6619{
6620 if(!buf)
6621 return 0;
6622
6623 return buf->use;
6624}
6625
6626/**
6627 * xmlBufferResize:
6628 * @buf: the buffer to resize
6629 * @size: the desired size
6630 *
Daniel Veillardd1640922001-12-17 15:30:10 +00006631 * Resize a buffer to accommodate minimum size of @size.
Owen Taylor3473f882001-02-23 17:55:21 +00006632 *
6633 * Returns 0 in case of problems, 1 otherwise
6634 */
6635int
6636xmlBufferResize(xmlBufferPtr buf, unsigned int size)
6637{
6638 unsigned int newSize;
6639 xmlChar* rebuf = NULL;
6640
Daniel Veillard53350552003-09-18 13:35:51 +00006641 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0);
6642
Owen Taylor3473f882001-02-23 17:55:21 +00006643 /*take care of empty case*/
6644 newSize = (buf->size ? buf->size*2 : size);
6645
6646 /* Don't resize if we don't have to */
6647 if (size < buf->size)
6648 return 1;
6649
6650 /* figure out new size */
6651 switch (buf->alloc){
6652 case XML_BUFFER_ALLOC_DOUBLEIT:
6653 while (size > newSize) newSize *= 2;
6654 break;
6655 case XML_BUFFER_ALLOC_EXACT:
6656 newSize = size+10;
6657 break;
6658 default:
6659 newSize = size+10;
6660 break;
6661 }
6662
6663 if (buf->content == NULL)
Daniel Veillard3c908dc2003-04-19 00:07:51 +00006664 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006665 else if (buf->size - buf->use < 100) {
Owen Taylor3473f882001-02-23 17:55:21 +00006666 rebuf = (xmlChar *) xmlRealloc(buf->content,
6667 newSize * sizeof(xmlChar));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006668 } else {
6669 /*
6670 * if we are reallocating a buffer far from being full, it's
6671 * better to make a new allocation and copy only the used range
6672 * and free the old one.
6673 */
6674 rebuf = (xmlChar *) xmlMallocAtomic(newSize * sizeof(xmlChar));
6675 if (rebuf != NULL) {
6676 memcpy(rebuf, buf->content, buf->use);
6677 xmlFree(buf->content);
6678 }
Daniel Veillarde5984082003-08-19 22:21:13 +00006679 rebuf[buf->use] = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006680 }
Owen Taylor3473f882001-02-23 17:55:21 +00006681 if (rebuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006682 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006683 return 0;
6684 }
6685 buf->content = rebuf;
6686 buf->size = newSize;
6687
6688 return 1;
6689}
6690
6691/**
6692 * xmlBufferAdd:
6693 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006694 * @str: the #xmlChar string
6695 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006696 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006697 * Add a string range to an XML buffer. if len == -1, the length of
Owen Taylor3473f882001-02-23 17:55:21 +00006698 * str is recomputed.
6699 */
6700void
6701xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) {
6702 unsigned int needSize;
6703
6704 if (str == NULL) {
6705#ifdef DEBUG_BUFFER
6706 xmlGenericError(xmlGenericErrorContext,
6707 "xmlBufferAdd: str == NULL\n");
6708#endif
6709 return;
6710 }
Daniel Veillard53350552003-09-18 13:35:51 +00006711 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006712 if (len < -1) {
6713#ifdef DEBUG_BUFFER
6714 xmlGenericError(xmlGenericErrorContext,
6715 "xmlBufferAdd: len < 0\n");
6716#endif
6717 return;
6718 }
6719 if (len == 0) return;
6720
6721 if (len < 0)
6722 len = xmlStrlen(str);
6723
6724 if (len <= 0) return;
6725
6726 needSize = buf->use + len + 2;
6727 if (needSize > buf->size){
6728 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006729 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006730 return;
6731 }
6732 }
6733
6734 memmove(&buf->content[buf->use], str, len*sizeof(xmlChar));
6735 buf->use += len;
6736 buf->content[buf->use] = 0;
6737}
6738
6739/**
6740 * xmlBufferAddHead:
6741 * @buf: the buffer
Daniel Veillardd1640922001-12-17 15:30:10 +00006742 * @str: the #xmlChar string
6743 * @len: the number of #xmlChar to add
Owen Taylor3473f882001-02-23 17:55:21 +00006744 *
6745 * Add a string range to the beginning of an XML buffer.
Daniel Veillard60087f32001-10-10 09:45:09 +00006746 * if len == -1, the length of @str is recomputed.
Owen Taylor3473f882001-02-23 17:55:21 +00006747 */
6748void
6749xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) {
6750 unsigned int needSize;
6751
Daniel Veillard53350552003-09-18 13:35:51 +00006752 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006753 if (str == NULL) {
6754#ifdef DEBUG_BUFFER
6755 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006756 "xmlBufferAddHead: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006757#endif
6758 return;
6759 }
6760 if (len < -1) {
6761#ifdef DEBUG_BUFFER
6762 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006763 "xmlBufferAddHead: len < 0\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006764#endif
6765 return;
6766 }
6767 if (len == 0) return;
6768
6769 if (len < 0)
6770 len = xmlStrlen(str);
6771
6772 if (len <= 0) return;
6773
6774 needSize = buf->use + len + 2;
6775 if (needSize > buf->size){
6776 if (!xmlBufferResize(buf, needSize)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006777 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006778 return;
6779 }
6780 }
6781
6782 memmove(&buf->content[len], &buf->content[0], buf->use * sizeof(xmlChar));
6783 memmove(&buf->content[0], str, len * sizeof(xmlChar));
6784 buf->use += len;
6785 buf->content[buf->use] = 0;
6786}
6787
6788/**
6789 * xmlBufferCat:
6790 * @buf: the buffer to dump
Daniel Veillardd1640922001-12-17 15:30:10 +00006791 * @str: the #xmlChar string
Owen Taylor3473f882001-02-23 17:55:21 +00006792 *
6793 * Append a zero terminated string to an XML buffer.
6794 */
6795void
6796xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) {
Daniel Veillard53350552003-09-18 13:35:51 +00006797 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006798 if (str != NULL)
6799 xmlBufferAdd(buf, str, -1);
6800}
6801
6802/**
6803 * xmlBufferCCat:
6804 * @buf: the buffer to dump
6805 * @str: the C char string
6806 *
6807 * Append a zero terminated C string to an XML buffer.
6808 */
6809void
6810xmlBufferCCat(xmlBufferPtr buf, const char *str) {
6811 const char *cur;
6812
Daniel Veillard53350552003-09-18 13:35:51 +00006813 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006814 if (str == NULL) {
6815#ifdef DEBUG_BUFFER
6816 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00006817 "xmlBufferCCat: str == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006818#endif
6819 return;
6820 }
6821 for (cur = str;*cur != 0;cur++) {
6822 if (buf->use + 10 >= buf->size) {
6823 if (!xmlBufferResize(buf, buf->use+10)){
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006824 xmlTreeErrMemory("growing buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00006825 return;
6826 }
6827 }
6828 buf->content[buf->use++] = *cur;
6829 }
6830 buf->content[buf->use] = 0;
6831}
6832
6833/**
6834 * xmlBufferWriteCHAR:
6835 * @buf: the XML buffer
6836 * @string: the string to add
6837 *
6838 * routine which manages and grows an output buffer. This one adds
6839 * xmlChars at the end of the buffer.
6840 */
6841void
Daniel Veillard53350552003-09-18 13:35:51 +00006842xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) {
6843 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006844 xmlBufferCat(buf, string);
6845}
6846
6847/**
6848 * xmlBufferWriteChar:
6849 * @buf: the XML buffer output
6850 * @string: the string to add
6851 *
6852 * routine which manage and grows an output buffer. This one add
6853 * C chars at the end of the array.
6854 */
6855void
6856xmlBufferWriteChar(xmlBufferPtr buf, const char *string) {
Daniel Veillard53350552003-09-18 13:35:51 +00006857 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006858 xmlBufferCCat(buf, string);
6859}
6860
6861
6862/**
6863 * xmlBufferWriteQuotedString:
6864 * @buf: the XML buffer output
6865 * @string: the string to add
6866 *
6867 * routine which manage and grows an output buffer. This one writes
Daniel Veillardd1640922001-12-17 15:30:10 +00006868 * a quoted or double quoted #xmlChar string, checking first if it holds
Owen Taylor3473f882001-02-23 17:55:21 +00006869 * quote or double-quotes internally
6870 */
6871void
6872xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) {
Daniel Veillard39057f42003-08-04 01:33:43 +00006873 const xmlChar *cur, *base;
Daniel Veillard53350552003-09-18 13:35:51 +00006874 if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return;
Daniel Veillard39057f42003-08-04 01:33:43 +00006875 if (xmlStrchr(string, '\"')) {
Daniel Veillard20aa0fb2003-08-04 19:43:15 +00006876 if (xmlStrchr(string, '\'')) {
Owen Taylor3473f882001-02-23 17:55:21 +00006877#ifdef DEBUG_BUFFER
6878 xmlGenericError(xmlGenericErrorContext,
6879 "xmlBufferWriteQuotedString: string contains quote and double-quotes !\n");
6880#endif
Daniel Veillard39057f42003-08-04 01:33:43 +00006881 xmlBufferCCat(buf, "\"");
6882 base = cur = string;
6883 while(*cur != 0){
6884 if(*cur == '"'){
6885 if (base != cur)
6886 xmlBufferAdd(buf, base, cur - base);
6887 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
6888 cur++;
6889 base = cur;
6890 }
6891 else {
6892 cur++;
6893 }
6894 }
6895 if (base != cur)
6896 xmlBufferAdd(buf, base, cur - base);
6897 xmlBufferCCat(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00006898 }
Daniel Veillard39057f42003-08-04 01:33:43 +00006899 else{
6900 xmlBufferCCat(buf, "\'");
6901 xmlBufferCat(buf, string);
6902 xmlBufferCCat(buf, "\'");
6903 }
Owen Taylor3473f882001-02-23 17:55:21 +00006904 } else {
6905 xmlBufferCCat(buf, "\"");
6906 xmlBufferCat(buf, string);
6907 xmlBufferCCat(buf, "\"");
6908 }
6909}
6910
6911
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00006912#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006913/************************************************************************
6914 * *
Daniel Veillarde2238d52003-10-09 13:14:55 +00006915 * Output error handlers *
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006916 * *
6917 ************************************************************************/
6918/**
6919 * xmlSaveErrMemory:
6920 * @extra: extra informations
6921 *
6922 * Handle an out of memory condition
6923 */
6924static void
6925xmlSaveErrMemory(const char *extra)
6926{
6927 __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
6928}
6929
6930/**
6931 * xmlSaveErr:
6932 * @code: the error number
6933 * @node: the location of the error.
6934 * @extra: extra informations
6935 *
6936 * Handle an out of memory condition
6937 */
6938static void
6939xmlSaveErr(int code, xmlNodePtr node, const char *extra)
6940{
6941 const char *msg = NULL;
6942
6943 switch(code) {
6944 case XML_SAVE_NOT_UTF8:
6945 msg = "string is not in UTF-8";
6946 break;
6947 case XML_SAVE_CHAR_INVALID:
6948 msg = "invalid character value";
6949 break;
6950 case XML_SAVE_UNKNOWN_ENCODING:
6951 msg = "unknown encoding %s";
6952 break;
Daniel Veillarde2238d52003-10-09 13:14:55 +00006953 case XML_SAVE_NO_DOCTYPE:
6954 msg = "document has no DOCTYPE";
6955 break;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006956 default:
6957 msg = "unexpected error number";
6958 }
Daniel Veillarde2238d52003-10-09 13:14:55 +00006959 __xmlSimpleError(XML_FROM_OUTPUT, code, node, msg, extra);
Daniel Veillard18ec16e2003-10-07 23:16:40 +00006960}
6961/************************************************************************
6962 * *
Owen Taylor3473f882001-02-23 17:55:21 +00006963 * Dumping XML tree content to a simple buffer *
6964 * *
6965 ************************************************************************/
6966
Owen Taylor3473f882001-02-23 17:55:21 +00006967/**
Daniel Veillarda6d05382002-02-13 13:07:41 +00006968 * xmlAttrSerializeContent:
6969 * @buf: the XML buffer output
6970 * @doc: the document
6971 * @attr: the attribute pointer
6972 *
6973 * Serialize the attribute in the buffer
6974 */
6975static void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006976xmlAttrSerializeContent(xmlBufferPtr buf, xmlDocPtr doc, xmlAttrPtr attr)
6977{
Daniel Veillarda6d05382002-02-13 13:07:41 +00006978 const xmlChar *cur, *base;
6979 xmlNodePtr children;
6980
6981 children = attr->children;
6982 while (children != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00006983 switch (children->type) {
6984 case XML_TEXT_NODE:
6985 base = cur = children->content;
6986 while (*cur != 0) {
6987 if (*cur == '\n') {
6988 if (base != cur)
6989 xmlBufferAdd(buf, base, cur - base);
6990 xmlBufferAdd(buf, BAD_CAST "&#10;", 5);
6991 cur++;
6992 base = cur;
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006993 } else if (*cur == '\r') {
6994 if (base != cur)
6995 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00006996 xmlBufferAdd(buf, BAD_CAST "&#13;", 5);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00006997 cur++;
6998 base = cur;
6999 } else if (*cur == '\t') {
7000 if (base != cur)
7001 xmlBufferAdd(buf, base, cur - base);
Daniel Veillardc64b8e92003-02-24 11:47:13 +00007002 xmlBufferAdd(buf, BAD_CAST "&#9;", 4);
Daniel Veillard0046c0f2003-02-23 13:52:30 +00007003 cur++;
7004 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007005#if 0
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007006 } else if (*cur == '\'') {
7007 if (base != cur)
7008 xmlBufferAdd(buf, base, cur - base);
7009 xmlBufferAdd(buf, BAD_CAST "&apos;", 6);
7010 cur++;
7011 base = cur;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007012#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007013 } else if (*cur == '"') {
7014 if (base != cur)
7015 xmlBufferAdd(buf, base, cur - base);
7016 xmlBufferAdd(buf, BAD_CAST "&quot;", 6);
7017 cur++;
7018 base = cur;
7019 } else if (*cur == '<') {
7020 if (base != cur)
7021 xmlBufferAdd(buf, base, cur - base);
7022 xmlBufferAdd(buf, BAD_CAST "&lt;", 4);
7023 cur++;
7024 base = cur;
7025 } else if (*cur == '>') {
7026 if (base != cur)
7027 xmlBufferAdd(buf, base, cur - base);
7028 xmlBufferAdd(buf, BAD_CAST "&gt;", 4);
7029 cur++;
7030 base = cur;
7031 } else if (*cur == '&') {
7032 if (base != cur)
7033 xmlBufferAdd(buf, base, cur - base);
7034 xmlBufferAdd(buf, BAD_CAST "&amp;", 5);
7035 cur++;
7036 base = cur;
7037 } else if ((*cur >= 0x80) && ((doc == NULL) ||
7038 (doc->encoding ==
7039 NULL))) {
7040 /*
7041 * We assume we have UTF-8 content.
7042 */
7043 char tmp[10];
7044 int val = 0, l = 1;
Daniel Veillarda6d05382002-02-13 13:07:41 +00007045
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007046 if (base != cur)
7047 xmlBufferAdd(buf, base, cur - base);
7048 if (*cur < 0xC0) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007049 xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr,
7050 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007051 if (doc != NULL)
7052 doc->encoding =
7053 xmlStrdup(BAD_CAST "ISO-8859-1");
7054 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
7055 tmp[sizeof(tmp) - 1] = 0;
7056 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7057 cur++;
7058 base = cur;
7059 continue;
7060 } else if (*cur < 0xE0) {
7061 val = (cur[0]) & 0x1F;
7062 val <<= 6;
7063 val |= (cur[1]) & 0x3F;
7064 l = 2;
7065 } else if (*cur < 0xF0) {
7066 val = (cur[0]) & 0x0F;
7067 val <<= 6;
7068 val |= (cur[1]) & 0x3F;
7069 val <<= 6;
7070 val |= (cur[2]) & 0x3F;
7071 l = 3;
7072 } else if (*cur < 0xF8) {
7073 val = (cur[0]) & 0x07;
7074 val <<= 6;
7075 val |= (cur[1]) & 0x3F;
7076 val <<= 6;
7077 val |= (cur[2]) & 0x3F;
7078 val <<= 6;
7079 val |= (cur[3]) & 0x3F;
7080 l = 4;
7081 }
7082 if ((l == 1) || (!IS_CHAR(val))) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007083 xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr,
7084 NULL);
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007085 if (doc != NULL)
7086 doc->encoding =
7087 xmlStrdup(BAD_CAST "ISO-8859-1");
7088 snprintf(tmp, sizeof(tmp), "&#%d;", *cur);
7089 tmp[sizeof(tmp) - 1] = 0;
7090 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7091 cur++;
7092 base = cur;
7093 continue;
7094 }
7095 /*
7096 * We could do multiple things here. Just save
7097 * as a char ref
7098 */
7099 snprintf(tmp, sizeof(tmp), "&#x%X;", val);
7100 tmp[sizeof(tmp) - 1] = 0;
7101 xmlBufferAdd(buf, (xmlChar *) tmp, -1);
7102 cur += l;
7103 base = cur;
7104 } else {
7105 cur++;
7106 }
7107 }
7108 if (base != cur)
7109 xmlBufferAdd(buf, base, cur - base);
7110 break;
7111 case XML_ENTITY_REF_NODE:
7112 xmlBufferAdd(buf, BAD_CAST "&", 1);
7113 xmlBufferAdd(buf, children->name,
7114 xmlStrlen(children->name));
7115 xmlBufferAdd(buf, BAD_CAST ";", 1);
7116 break;
7117 default:
7118 /* should not happen unless we have a badly built tree */
7119 break;
7120 }
7121 children = children->next;
Owen Taylor3473f882001-02-23 17:55:21 +00007122 }
7123}
7124
7125/**
7126 * xmlNodeDump:
7127 * @buf: the XML buffer output
7128 * @doc: the document
7129 * @cur: the current node
7130 * @level: the imbrication level for indenting
7131 * @format: is formatting allowed
7132 *
7133 * Dump an XML node, recursive behaviour,children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007134 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007135 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007136 *
7137 * Returns the number of bytes written to the buffer or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00007138 */
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007139int
Owen Taylor3473f882001-02-23 17:55:21 +00007140xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007141 int format)
7142{
7143 unsigned int use;
7144 int ret;
7145 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007146
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007147 xmlInitParser();
7148
Owen Taylor3473f882001-02-23 17:55:21 +00007149 if (cur == NULL) {
7150#ifdef DEBUG_TREE
7151 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007152 "xmlNodeDump : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007153#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007154 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007155 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007156 if (buf == NULL) {
7157#ifdef DEBUG_TREE
7158 xmlGenericError(xmlGenericErrorContext,
7159 "xmlNodeDump : buf == NULL\n");
7160#endif
7161 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007162 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007163 outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
7164 if (outbuf == NULL) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00007165 xmlSaveErrMemory("creating buffer");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007166 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00007167 }
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007168 memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
7169 outbuf->buffer = buf;
7170 outbuf->encoder = NULL;
7171 outbuf->writecallback = NULL;
7172 outbuf->closecallback = NULL;
7173 outbuf->context = NULL;
7174 outbuf->written = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007175
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007176 use = buf->use;
7177 xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL);
7178 xmlFree(outbuf);
7179 ret = buf->use - use;
7180 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00007181}
7182
7183/**
7184 * xmlElemDump:
7185 * @f: the FILE * for the output
7186 * @doc: the document
7187 * @cur: the current node
7188 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007189 * Dump an XML/HTML node, recursive behaviour, children are printed too.
Owen Taylor3473f882001-02-23 17:55:21 +00007190 */
7191void
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007192xmlElemDump(FILE * f, xmlDocPtr doc, xmlNodePtr cur)
7193{
7194 xmlOutputBufferPtr outbuf;
Owen Taylor3473f882001-02-23 17:55:21 +00007195
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007196 xmlInitParser();
7197
Owen Taylor3473f882001-02-23 17:55:21 +00007198 if (cur == NULL) {
7199#ifdef DEBUG_TREE
7200 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007201 "xmlElemDump : cur == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007202#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007203 return;
Owen Taylor3473f882001-02-23 17:55:21 +00007204 }
Owen Taylor3473f882001-02-23 17:55:21 +00007205#ifdef DEBUG_TREE
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007206 if (doc == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00007207 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007208 "xmlElemDump : doc == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007209 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007210#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007211
7212 outbuf = xmlOutputBufferCreateFile(f, NULL);
7213 if (outbuf == NULL)
7214 return;
7215 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007216#ifdef LIBXML_HTML_ENABLED
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007217 htmlNodeDumpOutput(outbuf, doc, cur, NULL);
7218#else
Daniel Veillard2189b592003-10-21 00:08:42 +00007219 xmlSaveErr(XML_ERR_INTERNAL_ERROR, cur, "HTML support not compiled in\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007220#endif /* LIBXML_HTML_ENABLED */
7221 } else
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007222 xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
7223 xmlOutputBufferClose(outbuf);
Owen Taylor3473f882001-02-23 17:55:21 +00007224}
7225
7226/************************************************************************
7227 * *
7228 * Dumping XML tree content to an I/O output buffer *
7229 * *
7230 ************************************************************************/
7231
Daniel Veillard4432df22003-09-28 18:58:27 +00007232#ifdef LIBXML_HTML_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007233static void
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007234xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7235 int level, int format, const char *encoding);
Daniel Veillard4432df22003-09-28 18:58:27 +00007236#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007237static void
Owen Taylor3473f882001-02-23 17:55:21 +00007238xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7239 int level, int format, const char *encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007240static void
7241xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7242 xmlNodePtr cur, int level, int format, const char *encoding);
7243
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007244void xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur);
7245
Owen Taylor3473f882001-02-23 17:55:21 +00007246/**
7247 * xmlNsDumpOutput:
7248 * @buf: the XML buffer output
7249 * @cur: a namespace
7250 *
7251 * Dump a local Namespace definition.
7252 * Should be called in the context of attributes dumps.
7253 */
7254static void
7255xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7256 if (cur == NULL) {
7257#ifdef DEBUG_TREE
7258 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007259 "xmlNsDumpOutput : Ns == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007260#endif
7261 return;
7262 }
7263 if ((cur->type == XML_LOCAL_NAMESPACE) && (cur->href != NULL)) {
Daniel Veillard6f46f6c2002-08-01 12:22:24 +00007264 if (xmlStrEqual(cur->prefix, BAD_CAST "xml"))
7265 return;
7266
Owen Taylor3473f882001-02-23 17:55:21 +00007267 /* Within the context of an element attributes */
7268 if (cur->prefix != NULL) {
7269 xmlOutputBufferWriteString(buf, " xmlns:");
7270 xmlOutputBufferWriteString(buf, (const char *)cur->prefix);
7271 } else
7272 xmlOutputBufferWriteString(buf, " xmlns");
7273 xmlOutputBufferWriteString(buf, "=");
7274 xmlBufferWriteQuotedString(buf->buffer, cur->href);
7275 }
7276}
7277
7278/**
7279 * xmlNsListDumpOutput:
7280 * @buf: the XML buffer output
7281 * @cur: the first namespace
7282 *
7283 * Dump a list of local Namespace definitions.
7284 * Should be called in the context of attributes dumps.
7285 */
Daniel Veillard5ecaf7f2003-01-09 13:19:33 +00007286void
Owen Taylor3473f882001-02-23 17:55:21 +00007287xmlNsListDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur) {
7288 while (cur != NULL) {
7289 xmlNsDumpOutput(buf, cur);
7290 cur = cur->next;
7291 }
7292}
7293
7294/**
7295 * xmlDtdDumpOutput:
7296 * @buf: the XML buffer output
7297 * @doc: the document
7298 * @encoding: an optional encoding string
7299 *
7300 * Dump the XML document DTD, if any.
7301 */
7302static void
7303xmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDtdPtr dtd, const char *encoding) {
7304 if (dtd == NULL) {
7305#ifdef DEBUG_TREE
7306 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007307 "xmlDtdDumpOutput : no internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007308#endif
7309 return;
7310 }
7311 xmlOutputBufferWriteString(buf, "<!DOCTYPE ");
7312 xmlOutputBufferWriteString(buf, (const char *)dtd->name);
7313 if (dtd->ExternalID != NULL) {
7314 xmlOutputBufferWriteString(buf, " PUBLIC ");
7315 xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID);
7316 xmlOutputBufferWriteString(buf, " ");
7317 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7318 } else if (dtd->SystemID != NULL) {
7319 xmlOutputBufferWriteString(buf, " SYSTEM ");
7320 xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID);
7321 }
7322 if ((dtd->entities == NULL) && (dtd->elements == NULL) &&
7323 (dtd->attributes == NULL) && (dtd->notations == NULL)) {
7324 xmlOutputBufferWriteString(buf, ">");
7325 return;
7326 }
7327 xmlOutputBufferWriteString(buf, " [\n");
7328 xmlNodeListDumpOutput(buf, dtd->doc, dtd->children, -1, 0, encoding);
7329 xmlOutputBufferWriteString(buf, "]>");
7330}
7331
7332/**
7333 * xmlAttrDumpOutput:
7334 * @buf: the XML buffer output
7335 * @doc: the document
7336 * @cur: the attribute pointer
7337 * @encoding: an optional encoding string
7338 *
7339 * Dump an XML attribute
7340 */
7341static void
7342xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00007343 const char *encoding ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00007344 if (cur == NULL) {
7345#ifdef DEBUG_TREE
7346 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007347 "xmlAttrDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007348#endif
7349 return;
7350 }
7351 xmlOutputBufferWriteString(buf, " ");
7352 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7353 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7354 xmlOutputBufferWriteString(buf, ":");
7355 }
7356 xmlOutputBufferWriteString(buf, (const char *)cur->name);
Daniel Veillarda6d05382002-02-13 13:07:41 +00007357 xmlOutputBufferWriteString(buf, "=\"");
7358 xmlAttrSerializeContent(buf->buffer, doc, cur);
7359 xmlOutputBufferWriteString(buf, "\"");
Owen Taylor3473f882001-02-23 17:55:21 +00007360}
7361
7362/**
7363 * xmlAttrListDumpOutput:
7364 * @buf: the XML buffer output
7365 * @doc: the document
7366 * @cur: the first attribute pointer
7367 * @encoding: an optional encoding string
7368 *
7369 * Dump a list of XML attributes
7370 */
7371static void
7372xmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7373 xmlAttrPtr cur, const char *encoding) {
7374 if (cur == NULL) {
7375#ifdef DEBUG_TREE
7376 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007377 "xmlAttrListDumpOutput : property == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007378#endif
7379 return;
7380 }
7381 while (cur != NULL) {
7382 xmlAttrDumpOutput(buf, doc, cur, encoding);
7383 cur = cur->next;
7384 }
7385}
7386
7387
7388
7389/**
7390 * xmlNodeListDumpOutput:
7391 * @buf: the XML buffer output
7392 * @doc: the document
7393 * @cur: the first node
7394 * @level: the imbrication level for indenting
7395 * @format: is formatting allowed
7396 * @encoding: an optional encoding string
7397 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007398 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007399 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007400 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007401 */
7402static void
7403xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7404 xmlNodePtr cur, int level, int format, const char *encoding) {
7405 int i;
7406
7407 if (cur == NULL) {
7408#ifdef DEBUG_TREE
7409 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007410 "xmlNodeListDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007411#endif
7412 return;
7413 }
7414 while (cur != NULL) {
7415 if ((format) && (xmlIndentTreeOutput) &&
7416 (cur->type == XML_ELEMENT_NODE))
7417 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007418 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007419 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007420 if (format) {
7421 xmlOutputBufferWriteString(buf, "\n");
7422 }
7423 cur = cur->next;
7424 }
7425}
7426
7427/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007428 * xmlNodeDumpOutputInternal:
Owen Taylor3473f882001-02-23 17:55:21 +00007429 * @buf: the XML buffer output
7430 * @doc: the document
7431 * @cur: the current node
7432 * @level: the imbrication level for indenting
7433 * @format: is formatting allowed
7434 * @encoding: an optional encoding string
7435 *
Daniel Veillardd1640922001-12-17 15:30:10 +00007436 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007437 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007438 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007439 */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007440static void
7441xmlNodeDumpOutputInternal(xmlOutputBufferPtr buf, xmlDocPtr doc,
7442 xmlNodePtr cur, int level, int format, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00007443 int i;
7444 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00007445 xmlChar *start, *end;
Owen Taylor3473f882001-02-23 17:55:21 +00007446
7447 if (cur == NULL) {
7448#ifdef DEBUG_TREE
7449 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd1640922001-12-17 15:30:10 +00007450 "xmlNodeDumpOutput : node == NULL\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007451#endif
7452 return;
7453 }
7454 if (cur->type == XML_XINCLUDE_START)
7455 return;
7456 if (cur->type == XML_XINCLUDE_END)
7457 return;
7458 if (cur->type == XML_DTD_NODE) {
7459 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
7460 return;
7461 }
Daniel Veillardcec50a62003-10-28 13:26:51 +00007462 if (cur->type == XML_DOCUMENT_FRAG_NODE) {
7463 xmlNodeListDumpOutput(buf, doc, cur->children, level, format, encoding);
7464 return;
7465 }
Owen Taylor3473f882001-02-23 17:55:21 +00007466 if (cur->type == XML_ELEMENT_DECL) {
7467 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
7468 return;
7469 }
7470 if (cur->type == XML_ATTRIBUTE_DECL) {
7471 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
7472 return;
7473 }
7474 if (cur->type == XML_ENTITY_DECL) {
7475 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
7476 return;
7477 }
7478 if (cur->type == XML_TEXT_NODE) {
7479 if (cur->content != NULL) {
7480 if ((cur->name == xmlStringText) ||
7481 (cur->name != xmlStringTextNoenc)) {
7482 xmlChar *buffer;
7483
Owen Taylor3473f882001-02-23 17:55:21 +00007484 if (encoding == NULL)
7485 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7486 else
7487 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007488 if (buffer != NULL) {
7489 xmlOutputBufferWriteString(buf, (const char *)buffer);
7490 xmlFree(buffer);
7491 }
7492 } else {
7493 /*
7494 * Disable escaping, needed for XSLT
7495 */
Owen Taylor3473f882001-02-23 17:55:21 +00007496 xmlOutputBufferWriteString(buf, (const char *) cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007497 }
7498 }
7499
7500 return;
7501 }
7502 if (cur->type == XML_PI_NODE) {
7503 if (cur->content != NULL) {
7504 xmlOutputBufferWriteString(buf, "<?");
7505 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7506 if (cur->content != NULL) {
7507 xmlOutputBufferWriteString(buf, " ");
Owen Taylor3473f882001-02-23 17:55:21 +00007508 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007509 }
7510 xmlOutputBufferWriteString(buf, "?>");
7511 } else {
7512 xmlOutputBufferWriteString(buf, "<?");
7513 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7514 xmlOutputBufferWriteString(buf, "?>");
7515 }
7516 return;
7517 }
7518 if (cur->type == XML_COMMENT_NODE) {
7519 if (cur->content != NULL) {
7520 xmlOutputBufferWriteString(buf, "<!--");
Owen Taylor3473f882001-02-23 17:55:21 +00007521 xmlOutputBufferWriteString(buf, (const char *)cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007522 xmlOutputBufferWriteString(buf, "-->");
7523 }
7524 return;
7525 }
7526 if (cur->type == XML_ENTITY_REF_NODE) {
7527 xmlOutputBufferWriteString(buf, "&");
7528 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7529 xmlOutputBufferWriteString(buf, ";");
7530 return;
7531 }
7532 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00007533 start = end = cur->content;
7534 while (*end != '\0') {
7535 if ((*end == ']') && (*(end + 1) == ']') && (*(end + 2) == '>')) {
7536 end = end + 2;
7537 xmlOutputBufferWriteString(buf, "<![CDATA[");
7538 xmlOutputBufferWrite(buf, end - start, (const char *)start);
7539 xmlOutputBufferWriteString(buf, "]]>");
7540 start = end;
7541 }
7542 end++;
7543 }
7544 if (start != end) {
7545 xmlOutputBufferWriteString(buf, "<![CDATA[");
7546 xmlOutputBufferWriteString(buf, (const char *)start);
7547 xmlOutputBufferWriteString(buf, "]]>");
7548 }
Owen Taylor3473f882001-02-23 17:55:21 +00007549 return;
7550 }
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007551 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillarda1a9d042003-03-18 16:53:17 +00007552 xmlAttrDumpOutput(buf,doc, (xmlAttrPtr) cur,encoding);
Daniel Veillard7b72ee52003-02-27 23:24:53 +00007553 return;
7554 }
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007555 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007556 xmlNsDumpOutput(buf, (xmlNsPtr) cur);
Daniel Veillard6aa2f602003-02-10 00:01:56 +00007557 return;
7558 }
Owen Taylor3473f882001-02-23 17:55:21 +00007559
7560 if (format == 1) {
7561 tmp = cur->children;
7562 while (tmp != NULL) {
William M. Brack9ca682f2003-10-19 10:01:59 +00007563 if ((tmp->type == XML_TEXT_NODE) ||
7564 (tmp->type == XML_CDATA_SECTION_NODE) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007565 (tmp->type == XML_ENTITY_REF_NODE)) {
7566 format = 0;
7567 break;
7568 }
7569 tmp = tmp->next;
7570 }
7571 }
7572 xmlOutputBufferWriteString(buf, "<");
7573 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7574 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7575 xmlOutputBufferWriteString(buf, ":");
7576 }
7577
7578 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7579 if (cur->nsDef)
7580 xmlNsListDumpOutput(buf, cur->nsDef);
7581 if (cur->properties != NULL)
7582 xmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
7583
Daniel Veillard7db37732001-07-12 01:20:08 +00007584 if (((cur->type == XML_ELEMENT_NODE) || (cur->content == NULL)) &&
7585 (cur->children == NULL) && (!xmlSaveNoEmptyTags)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007586 xmlOutputBufferWriteString(buf, "/>");
7587 return;
7588 }
7589 xmlOutputBufferWriteString(buf, ">");
Daniel Veillard7db37732001-07-12 01:20:08 +00007590 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007591 xmlChar *buffer;
7592
Owen Taylor3473f882001-02-23 17:55:21 +00007593 if (encoding == NULL)
7594 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
7595 else
7596 buffer = xmlEncodeSpecialChars(doc, cur->content);
Owen Taylor3473f882001-02-23 17:55:21 +00007597 if (buffer != NULL) {
7598 xmlOutputBufferWriteString(buf, (const char *)buffer);
7599 xmlFree(buffer);
7600 }
7601 }
7602 if (cur->children != NULL) {
7603 if (format) xmlOutputBufferWriteString(buf, "\n");
7604 xmlNodeListDumpOutput(buf, doc, cur->children,
7605 (level >= 0?level+1:-1), format, encoding);
7606 if ((xmlIndentTreeOutput) && (format))
7607 for (i = 0;i < level;i++)
Aleksey Sanin23002562002-05-24 07:18:40 +00007608 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
Owen Taylor3473f882001-02-23 17:55:21 +00007609 }
7610 xmlOutputBufferWriteString(buf, "</");
7611 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
7612 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
7613 xmlOutputBufferWriteString(buf, ":");
7614 }
7615
7616 xmlOutputBufferWriteString(buf, (const char *)cur->name);
7617 xmlOutputBufferWriteString(buf, ">");
7618}
7619
7620/**
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007621 * xmlNodeDumpOutput:
7622 * @buf: the XML buffer output
7623 * @doc: the document
7624 * @cur: the current node
7625 * @level: the imbrication level for indenting
7626 * @format: is formatting allowed
7627 * @encoding: an optional encoding string
7628 *
7629 * Dump an XML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007630 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007631 * or xmlKeepBlanksDefault(0) was called
7632 */
7633void
7634xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007635 int level, int format, const char *encoding)
7636{
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007637#ifdef LIBXML_HTML_ENABLED
7638 xmlDtdPtr dtd;
7639 int is_xhtml = 0;
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007640#endif
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007641
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007642 xmlInitParser();
7643
7644#ifdef LIBXML_HTML_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007645 dtd = xmlGetIntSubset(doc);
7646 if (dtd != NULL) {
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007647 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7648 if (is_xhtml < 0)
7649 is_xhtml = 0;
7650 if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
7651 (cur->type == XML_ELEMENT_NODE) &&
7652 (xmlStrEqual(cur->name, BAD_CAST "html"))) {
7653 if (encoding != NULL)
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007654 htmlSetMetaEncoding((htmlDocPtr) doc,
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007655 (const xmlChar *) encoding);
7656 else
Daniel Veillardbeb70bd2002-12-18 14:53:54 +00007657 htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007658 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007659 }
7660
7661 if (is_xhtml)
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007662 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007663 else
7664#endif
Daniel Veillardebc4ca92002-11-27 11:43:05 +00007665 xmlNodeDumpOutputInternal(buf, doc, cur, level, format, encoding);
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007666}
7667
7668/**
Owen Taylor3473f882001-02-23 17:55:21 +00007669 * xmlDocContentDumpOutput:
7670 * @buf: the XML buffer output
7671 * @cur: the document
7672 * @encoding: an optional encoding string
7673 * @format: should formatting spaces been added
7674 *
7675 * Dump an XML document.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007676 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00007677 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00007678 */
7679static void
7680xmlDocContentDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr cur,
7681 const char *encoding, int format) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007682#ifdef LIBXML_HTML_ENABLED
7683 xmlDtdPtr dtd;
7684 int is_xhtml = 0;
7685#endif
7686
Daniel Veillard70bcb0e2003-08-08 14:00:28 +00007687 xmlInitParser();
7688
Owen Taylor3473f882001-02-23 17:55:21 +00007689 xmlOutputBufferWriteString(buf, "<?xml version=");
7690 if (cur->version != NULL)
7691 xmlBufferWriteQuotedString(buf->buffer, cur->version);
7692 else
7693 xmlOutputBufferWriteString(buf, "\"1.0\"");
7694 if (encoding == NULL) {
7695 if (cur->encoding != NULL)
7696 encoding = (const char *) cur->encoding;
7697 else if (cur->charset != XML_CHAR_ENCODING_UTF8)
7698 encoding = xmlGetCharEncodingName((xmlCharEncoding) cur->charset);
7699 }
7700 if (encoding != NULL) {
7701 xmlOutputBufferWriteString(buf, " encoding=");
7702 xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding);
7703 }
7704 switch (cur->standalone) {
7705 case 0:
7706 xmlOutputBufferWriteString(buf, " standalone=\"no\"");
7707 break;
7708 case 1:
7709 xmlOutputBufferWriteString(buf, " standalone=\"yes\"");
7710 break;
7711 }
7712 xmlOutputBufferWriteString(buf, "?>\n");
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007713
7714#ifdef LIBXML_HTML_ENABLED
7715 dtd = xmlGetIntSubset(cur);
7716 if (dtd != NULL) {
7717 is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
7718 if (is_xhtml < 0) is_xhtml = 0;
7719 }
7720 if (is_xhtml) {
7721 if (encoding != NULL)
7722 htmlSetMetaEncoding(cur, (const xmlChar *) encoding);
7723 else
7724 htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
7725 }
7726#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007727 if (cur->children != NULL) {
7728 xmlNodePtr child = cur->children;
7729
7730 while (child != NULL) {
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007731#ifdef LIBXML_HTML_ENABLED
7732 if (is_xhtml)
7733 xhtmlNodeDumpOutput(buf, cur, child, 0, format, encoding);
7734 else
7735#endif
7736 xmlNodeDumpOutputInternal(buf, cur, child, 0, format, encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00007737 xmlOutputBufferWriteString(buf, "\n");
7738 child = child->next;
7739 }
7740 }
7741}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007742#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007743
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007744#ifdef LIBXML_HTML_ENABLED
7745/************************************************************************
7746 * *
7747 * Functions specific to XHTML serialization *
7748 * *
7749 ************************************************************************/
7750
7751#define XHTML_STRICT_PUBLIC_ID BAD_CAST \
7752 "-//W3C//DTD XHTML 1.0 Strict//EN"
7753#define XHTML_STRICT_SYSTEM_ID BAD_CAST \
7754 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
7755#define XHTML_FRAME_PUBLIC_ID BAD_CAST \
7756 "-//W3C//DTD XHTML 1.0 Frameset//EN"
7757#define XHTML_FRAME_SYSTEM_ID BAD_CAST \
7758 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"
7759#define XHTML_TRANS_PUBLIC_ID BAD_CAST \
7760 "-//W3C//DTD XHTML 1.0 Transitional//EN"
7761#define XHTML_TRANS_SYSTEM_ID BAD_CAST \
7762 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
7763
7764#define XHTML_NS_NAME BAD_CAST "http://www.w3.org/1999/xhtml"
7765/**
7766 * xmlIsXHTML:
7767 * @systemID: the system identifier
7768 * @publicID: the public identifier
7769 *
7770 * Try to find if the document correspond to an XHTML DTD
7771 *
7772 * Returns 1 if true, 0 if not and -1 in case of error
7773 */
7774int
7775xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) {
7776 if ((systemID == NULL) && (publicID == NULL))
7777 return(-1);
7778 if (publicID != NULL) {
7779 if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1);
7780 if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1);
7781 if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1);
7782 }
7783 if (systemID != NULL) {
7784 if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1);
7785 if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1);
7786 if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1);
7787 }
7788 return(0);
7789}
7790
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007791#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007792/**
7793 * xhtmlIsEmpty:
7794 * @node: the node
7795 *
7796 * Check if a node is an empty xhtml node
7797 *
7798 * Returns 1 if the node is an empty node, 0 if not and -1 in case of error
7799 */
7800static int
7801xhtmlIsEmpty(xmlNodePtr node) {
7802 if (node == NULL)
7803 return(-1);
7804 if (node->type != XML_ELEMENT_NODE)
7805 return(0);
7806 if ((node->ns != NULL) && (!xmlStrEqual(node->ns->href, XHTML_NS_NAME)))
7807 return(0);
7808 if (node->children != NULL)
7809 return(0);
7810 switch (node->name[0]) {
7811 case 'a':
7812 if (xmlStrEqual(node->name, BAD_CAST "area"))
7813 return(1);
7814 return(0);
7815 case 'b':
7816 if (xmlStrEqual(node->name, BAD_CAST "br"))
7817 return(1);
7818 if (xmlStrEqual(node->name, BAD_CAST "base"))
7819 return(1);
7820 if (xmlStrEqual(node->name, BAD_CAST "basefont"))
7821 return(1);
7822 return(0);
7823 case 'c':
7824 if (xmlStrEqual(node->name, BAD_CAST "col"))
7825 return(1);
7826 return(0);
7827 case 'f':
7828 if (xmlStrEqual(node->name, BAD_CAST "frame"))
7829 return(1);
7830 return(0);
7831 case 'h':
7832 if (xmlStrEqual(node->name, BAD_CAST "hr"))
7833 return(1);
7834 return(0);
7835 case 'i':
7836 if (xmlStrEqual(node->name, BAD_CAST "img"))
7837 return(1);
7838 if (xmlStrEqual(node->name, BAD_CAST "input"))
7839 return(1);
7840 if (xmlStrEqual(node->name, BAD_CAST "isindex"))
7841 return(1);
7842 return(0);
7843 case 'l':
7844 if (xmlStrEqual(node->name, BAD_CAST "link"))
7845 return(1);
7846 return(0);
7847 case 'm':
7848 if (xmlStrEqual(node->name, BAD_CAST "meta"))
7849 return(1);
7850 return(0);
7851 case 'p':
7852 if (xmlStrEqual(node->name, BAD_CAST "param"))
7853 return(1);
7854 return(0);
7855 }
7856 return(0);
7857}
7858
7859/**
7860 * xhtmlAttrListDumpOutput:
7861 * @buf: the XML buffer output
7862 * @doc: the document
7863 * @cur: the first attribute pointer
7864 * @encoding: an optional encoding string
7865 *
7866 * Dump a list of XML attributes
7867 */
7868static void
7869xhtmlAttrListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7870 xmlAttrPtr cur, const char *encoding) {
7871 xmlAttrPtr xml_lang = NULL;
7872 xmlAttrPtr lang = NULL;
7873 xmlAttrPtr name = NULL;
7874 xmlAttrPtr id = NULL;
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007875 xmlNodePtr parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007876
7877 if (cur == NULL) {
7878#ifdef DEBUG_TREE
7879 xmlGenericError(xmlGenericErrorContext,
7880 "xmlAttrListDumpOutput : property == NULL\n");
7881#endif
7882 return;
7883 }
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007884 parent = cur->parent;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007885 while (cur != NULL) {
7886 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "id")))
7887 id = cur;
7888 else
7889 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "name")))
7890 name = cur;
7891 else
7892 if ((cur->ns == NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")))
7893 lang = cur;
7894 else
7895 if ((cur->ns != NULL) && (xmlStrEqual(cur->name, BAD_CAST "lang")) &&
7896 (xmlStrEqual(cur->ns->prefix, BAD_CAST "xml")))
7897 xml_lang = cur;
7898 else if ((cur->ns == NULL) &&
7899 ((cur->children == NULL) ||
7900 (cur->children->content == NULL) ||
7901 (cur->children->content[0] == 0)) &&
7902 (htmlIsBooleanAttr(cur->name))) {
7903 if (cur->children != NULL)
7904 xmlFreeNode(cur->children);
7905 cur->children = xmlNewText(cur->name);
7906 if (cur->children != NULL)
7907 cur->children->parent = (xmlNodePtr) cur;
7908 }
7909 xmlAttrDumpOutput(buf, doc, cur, encoding);
7910 cur = cur->next;
7911 }
7912 /*
7913 * C.8
7914 */
7915 if ((name != NULL) && (id == NULL)) {
Daniel Veillardfd7ce5f2003-02-10 16:12:39 +00007916 if ((parent != NULL) && (parent->name != NULL) &&
7917 ((xmlStrEqual(parent->name, BAD_CAST "a")) ||
7918 (xmlStrEqual(parent->name, BAD_CAST "p")) ||
7919 (xmlStrEqual(parent->name, BAD_CAST "div")) ||
7920 (xmlStrEqual(parent->name, BAD_CAST "img")) ||
7921 (xmlStrEqual(parent->name, BAD_CAST "map")) ||
7922 (xmlStrEqual(parent->name, BAD_CAST "applet")) ||
7923 (xmlStrEqual(parent->name, BAD_CAST "form")) ||
7924 (xmlStrEqual(parent->name, BAD_CAST "frame")) ||
7925 (xmlStrEqual(parent->name, BAD_CAST "iframe")))) {
7926 xmlOutputBufferWriteString(buf, " id=\"");
7927 xmlAttrSerializeContent(buf->buffer, doc, name);
7928 xmlOutputBufferWriteString(buf, "\"");
7929 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007930 }
7931 /*
7932 * C.7.
7933 */
7934 if ((lang != NULL) && (xml_lang == NULL)) {
7935 xmlOutputBufferWriteString(buf, " xml:lang=\"");
7936 xmlAttrSerializeContent(buf->buffer, doc, lang);
7937 xmlOutputBufferWriteString(buf, "\"");
7938 } else
7939 if ((xml_lang != NULL) && (lang == NULL)) {
7940 xmlOutputBufferWriteString(buf, " lang=\"");
7941 xmlAttrSerializeContent(buf->buffer, doc, xml_lang);
7942 xmlOutputBufferWriteString(buf, "\"");
7943 }
7944}
7945
7946/**
7947 * xhtmlNodeListDumpOutput:
7948 * @buf: the XML buffer output
7949 * @doc: the XHTML document
7950 * @cur: the first node
7951 * @level: the imbrication level for indenting
7952 * @format: is formatting allowed
7953 * @encoding: an optional encoding string
7954 *
7955 * Dump an XML node list, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007956 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007957 * or xmlKeepBlanksDefault(0) was called
7958 */
7959static void
7960xhtmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc,
7961 xmlNodePtr cur, int level, int format, const char *encoding) {
7962 int i;
7963
7964 if (cur == NULL) {
7965#ifdef DEBUG_TREE
7966 xmlGenericError(xmlGenericErrorContext,
7967 "xhtmlNodeListDumpOutput : node == NULL\n");
7968#endif
7969 return;
7970 }
7971 while (cur != NULL) {
7972 if ((format) && (xmlIndentTreeOutput) &&
7973 (cur->type == XML_ELEMENT_NODE))
7974 for (i = 0;i < level;i++)
7975 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
7976 xhtmlNodeDumpOutput(buf, doc, cur, level, format, encoding);
7977 if (format) {
7978 xmlOutputBufferWriteString(buf, "\n");
7979 }
7980 cur = cur->next;
7981 }
7982}
7983
7984/**
7985 * xhtmlNodeDumpOutput:
7986 * @buf: the XML buffer output
7987 * @doc: the XHTML document
7988 * @cur: the current node
7989 * @level: the imbrication level for indenting
7990 * @format: is formatting allowed
7991 * @encoding: an optional encoding string
7992 *
7993 * Dump an XHTML node, recursive behaviour, children are printed too.
Daniel Veillard7424eb62003-01-24 14:14:52 +00007994 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillardd5c2f922002-11-21 14:10:52 +00007995 * or xmlKeepBlanksDefault(0) was called
7996 */
7997static void
7998xhtmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur,
7999 int level, int format, const char *encoding) {
8000 int i;
8001 xmlNodePtr tmp;
Daniel Veillard9475a352003-09-26 12:47:50 +00008002 xmlChar *start, *end;
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008003
8004 if (cur == NULL) {
8005#ifdef DEBUG_TREE
8006 xmlGenericError(xmlGenericErrorContext,
8007 "xmlNodeDumpOutput : node == NULL\n");
8008#endif
8009 return;
8010 }
8011 if (cur->type == XML_XINCLUDE_START)
8012 return;
8013 if (cur->type == XML_XINCLUDE_END)
8014 return;
8015 if (cur->type == XML_DTD_NODE) {
8016 xmlDtdDumpOutput(buf, (xmlDtdPtr) cur, encoding);
8017 return;
8018 }
8019 if (cur->type == XML_ELEMENT_DECL) {
8020 xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur);
8021 return;
8022 }
8023 if (cur->type == XML_ATTRIBUTE_DECL) {
8024 xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur);
8025 return;
8026 }
8027 if (cur->type == XML_ENTITY_DECL) {
8028 xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur);
8029 return;
8030 }
8031 if (cur->type == XML_TEXT_NODE) {
8032 if (cur->content != NULL) {
8033 if ((cur->name == xmlStringText) ||
8034 (cur->name != xmlStringTextNoenc)) {
8035 xmlChar *buffer;
8036
8037 if (encoding == NULL)
8038 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
8039 else
8040 buffer = xmlEncodeSpecialChars(doc, cur->content);
8041 if (buffer != NULL) {
8042 xmlOutputBufferWriteString(buf, (const char *)buffer);
8043 xmlFree(buffer);
8044 }
8045 } else {
8046 /*
8047 * Disable escaping, needed for XSLT
8048 */
8049 xmlOutputBufferWriteString(buf, (const char *) cur->content);
8050 }
8051 }
8052
8053 return;
8054 }
8055 if (cur->type == XML_PI_NODE) {
8056 if (cur->content != NULL) {
8057 xmlOutputBufferWriteString(buf, "<?");
8058 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8059 if (cur->content != NULL) {
8060 xmlOutputBufferWriteString(buf, " ");
8061 xmlOutputBufferWriteString(buf, (const char *)cur->content);
8062 }
8063 xmlOutputBufferWriteString(buf, "?>");
8064 } else {
8065 xmlOutputBufferWriteString(buf, "<?");
8066 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8067 xmlOutputBufferWriteString(buf, "?>");
8068 }
8069 return;
8070 }
8071 if (cur->type == XML_COMMENT_NODE) {
8072 if (cur->content != NULL) {
8073 xmlOutputBufferWriteString(buf, "<!--");
8074 xmlOutputBufferWriteString(buf, (const char *)cur->content);
8075 xmlOutputBufferWriteString(buf, "-->");
8076 }
8077 return;
8078 }
8079 if (cur->type == XML_ENTITY_REF_NODE) {
8080 xmlOutputBufferWriteString(buf, "&");
8081 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8082 xmlOutputBufferWriteString(buf, ";");
8083 return;
8084 }
8085 if (cur->type == XML_CDATA_SECTION_NODE) {
Daniel Veillard9475a352003-09-26 12:47:50 +00008086 start = end = cur->content;
8087 while (*end != '\0') {
8088 if (*end == ']' && *(end + 1) == ']' && *(end + 2) == '>') {
8089 end = end + 2;
8090 xmlOutputBufferWriteString(buf, "<![CDATA[");
8091 xmlOutputBufferWrite(buf, end - start, (const char *)start);
8092 xmlOutputBufferWriteString(buf, "]]>");
8093 start = end;
8094 }
8095 end++;
8096 }
8097 if (start != end) {
8098 xmlOutputBufferWriteString(buf, "<![CDATA[");
8099 xmlOutputBufferWriteString(buf, (const char *)start);
8100 xmlOutputBufferWriteString(buf, "]]>");
8101 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008102 return;
8103 }
8104
8105 if (format == 1) {
8106 tmp = cur->children;
8107 while (tmp != NULL) {
8108 if ((tmp->type == XML_TEXT_NODE) ||
8109 (tmp->type == XML_ENTITY_REF_NODE)) {
8110 format = 0;
8111 break;
8112 }
8113 tmp = tmp->next;
8114 }
8115 }
8116 xmlOutputBufferWriteString(buf, "<");
8117 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8118 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8119 xmlOutputBufferWriteString(buf, ":");
8120 }
8121
8122 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8123 if (cur->nsDef)
8124 xmlNsListDumpOutput(buf, cur->nsDef);
8125 if ((xmlStrEqual(cur->name, BAD_CAST "html") &&
8126 (cur->ns == NULL) && (cur->nsDef == NULL))) {
8127 /*
8128 * 3.1.1. Strictly Conforming Documents A.3.1.1 3/
8129 */
8130 xmlOutputBufferWriteString(buf,
8131 " xmlns=\"http://www.w3.org/1999/xhtml\"");
8132 }
8133 if (cur->properties != NULL)
8134 xhtmlAttrListDumpOutput(buf, doc, cur->properties, encoding);
8135
8136 if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
8137 if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
8138 (xhtmlIsEmpty(cur) == 1)) {
8139 /*
8140 * C.2. Empty Elements
8141 */
8142 xmlOutputBufferWriteString(buf, " />");
8143 } else {
8144 /*
8145 * C.3. Element Minimization and Empty Element Content
8146 */
8147 xmlOutputBufferWriteString(buf, "></");
8148 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8149 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8150 xmlOutputBufferWriteString(buf, ":");
8151 }
8152 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8153 xmlOutputBufferWriteString(buf, ">");
8154 }
8155 return;
8156 }
8157 xmlOutputBufferWriteString(buf, ">");
8158 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
8159 xmlChar *buffer;
8160
8161 if (encoding == NULL)
8162 buffer = xmlEncodeEntitiesReentrant(doc, cur->content);
8163 else
8164 buffer = xmlEncodeSpecialChars(doc, cur->content);
8165 if (buffer != NULL) {
8166 xmlOutputBufferWriteString(buf, (const char *)buffer);
8167 xmlFree(buffer);
8168 }
8169 }
8170
8171 /*
8172 * 4.8. Script and Style elements
8173 */
8174 if ((cur->type == XML_ELEMENT_NODE) &&
8175 ((xmlStrEqual(cur->name, BAD_CAST "script")) ||
8176 (xmlStrEqual(cur->name, BAD_CAST "style"))) &&
8177 ((cur->ns == NULL) ||
8178 (xmlStrEqual(cur->ns->href, XHTML_NS_NAME)))) {
8179 xmlNodePtr child = cur->children;
8180
8181 while (child != NULL) {
8182 if ((child->type == XML_TEXT_NODE) ||
8183 (child->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard64b35282002-12-04 15:10:40 +00008184 /*
8185 * Apparently CDATA escaping for style just break on IE,
8186 * mozilla and galeon, so ...
8187 */
8188 if (xmlStrEqual(cur->name, BAD_CAST "style") &&
8189 (xmlStrchr(child->content, '<') == NULL) &&
8190 (xmlStrchr(child->content, '>') == NULL) &&
8191 (xmlStrchr(child->content, '&') == NULL)) {
8192 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8193 } else {
Daniel Veillard9475a352003-09-26 12:47:50 +00008194 start = end = child->content;
8195 while (*end != '\0') {
8196 if (*end == ']' &&
8197 *(end + 1) == ']' &&
8198 *(end + 2) == '>') {
8199 end = end + 2;
8200 xmlOutputBufferWriteString(buf, "<![CDATA[");
8201 xmlOutputBufferWrite(buf, end - start,
8202 (const char *)start);
8203 xmlOutputBufferWriteString(buf, "]]>");
8204 start = end;
8205 }
8206 end++;
8207 }
8208 if (start != end) {
8209 xmlOutputBufferWriteString(buf, "<![CDATA[");
8210 xmlOutputBufferWriteString(buf, (const char *)start);
8211 xmlOutputBufferWriteString(buf, "]]>");
8212 }
Daniel Veillard64b35282002-12-04 15:10:40 +00008213 }
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008214 } else {
8215 xhtmlNodeDumpOutput(buf, doc, child, 0, 0, encoding);
8216 }
8217 child = child->next;
8218 }
8219 } else if (cur->children != NULL) {
8220 if (format) xmlOutputBufferWriteString(buf, "\n");
8221 xhtmlNodeListDumpOutput(buf, doc, cur->children,
8222 (level >= 0?level+1:-1), format, encoding);
8223 if ((xmlIndentTreeOutput) && (format))
8224 for (i = 0;i < level;i++)
8225 xmlOutputBufferWriteString(buf, xmlTreeIndentString);
8226 }
8227 xmlOutputBufferWriteString(buf, "</");
8228 if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
8229 xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
8230 xmlOutputBufferWriteString(buf, ":");
8231 }
8232
8233 xmlOutputBufferWriteString(buf, (const char *)cur->name);
8234 xmlOutputBufferWriteString(buf, ">");
8235}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008236#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardd5c2f922002-11-21 14:10:52 +00008237#endif
8238
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008239#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008240/************************************************************************
8241 * *
8242 * Saving functions front-ends *
8243 * *
8244 ************************************************************************/
8245
8246/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00008247 * xmlDocDumpFormatMemoryEnc:
Owen Taylor3473f882001-02-23 17:55:21 +00008248 * @out_doc: Document to generate XML text from
8249 * @doc_txt_ptr: Memory pointer for allocated XML text
8250 * @doc_txt_len: Length of the generated XML text
8251 * @txt_encoding: Character encoding to use when generating XML text
8252 * @format: should formatting spaces been added
8253 *
8254 * Dump the current DOM tree into memory using the character encoding specified
8255 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008256 * allocated memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008257 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008258 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008259 */
8260
8261void
8262xmlDocDumpFormatMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008263 int * doc_txt_len, const char * txt_encoding,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008264 int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008265 int dummy = 0;
8266
Owen Taylor3473f882001-02-23 17:55:21 +00008267 xmlOutputBufferPtr out_buff = NULL;
8268 xmlCharEncodingHandlerPtr conv_hdlr = NULL;
8269
8270 if (doc_txt_len == NULL) {
8271 doc_txt_len = &dummy; /* Continue, caller just won't get length */
8272 }
8273
8274 if (doc_txt_ptr == NULL) {
8275 *doc_txt_len = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008276 return;
8277 }
8278
8279 *doc_txt_ptr = NULL;
8280 *doc_txt_len = 0;
8281
8282 if (out_doc == NULL) {
8283 /* No document, no output */
Owen Taylor3473f882001-02-23 17:55:21 +00008284 return;
8285 }
8286
8287 /*
8288 * Validate the encoding value, if provided.
8289 * This logic is copied from xmlSaveFileEnc.
8290 */
8291
8292 if (txt_encoding == NULL)
8293 txt_encoding = (const char *) out_doc->encoding;
8294 if (txt_encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008295 conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008296 if ( conv_hdlr == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008297 xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
8298 txt_encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008299 return;
8300 }
8301 }
Owen Taylor3473f882001-02-23 17:55:21 +00008302
8303 if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008304 xmlSaveErrMemory("creating buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00008305 return;
8306 }
8307
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008308 xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008309 xmlOutputBufferFlush(out_buff);
8310 if (out_buff->conv != NULL) {
8311 *doc_txt_len = out_buff->conv->use;
8312 *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len);
8313 } else {
8314 *doc_txt_len = out_buff->buffer->use;
8315 *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
8316 }
8317 (void)xmlOutputBufferClose(out_buff);
8318
8319 if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
8320 *doc_txt_len = 0;
Daniel Veillard18ec16e2003-10-07 23:16:40 +00008321 xmlSaveErrMemory("creating output");
Owen Taylor3473f882001-02-23 17:55:21 +00008322 }
8323
8324 return;
8325}
8326
8327/**
8328 * xmlDocDumpMemory:
8329 * @cur: the document
8330 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008331 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008332 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008333 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008334 * It's up to the caller to free the memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008335 */
8336void
8337xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) {
8338 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, 0);
8339}
8340
8341/**
8342 * xmlDocDumpFormatMemory:
8343 * @cur: the document
8344 * @mem: OUT: the memory pointer
Daniel Veillard60087f32001-10-10 09:45:09 +00008345 * @size: OUT: the memory length
Owen Taylor3473f882001-02-23 17:55:21 +00008346 * @format: should formatting spaces been added
8347 *
8348 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008349 * Dump an XML document in memory and return the #xmlChar * and it's size.
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008350 * It's up to the caller to free the memory with xmlFree().
Daniel Veillard7424eb62003-01-24 14:14:52 +00008351 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
Daniel Veillard4b3a84f2002-03-19 14:36:46 +00008352 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008353 */
8354void
8355xmlDocDumpFormatMemory(xmlDocPtr cur, xmlChar**mem, int *size, int format) {
8356 xmlDocDumpFormatMemoryEnc(cur, mem, size, NULL, format);
8357}
8358
8359/**
8360 * xmlDocDumpMemoryEnc:
8361 * @out_doc: Document to generate XML text from
8362 * @doc_txt_ptr: Memory pointer for allocated XML text
8363 * @doc_txt_len: Length of the generated XML text
8364 * @txt_encoding: Character encoding to use when generating XML text
8365 *
8366 * Dump the current DOM tree into memory using the character encoding specified
8367 * by the caller. Note it is up to the caller of this function to free the
Daniel Veillardbd9afb52002-09-25 22:25:35 +00008368 * allocated memory with xmlFree().
Owen Taylor3473f882001-02-23 17:55:21 +00008369 */
8370
8371void
8372xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
8373 int * doc_txt_len, const char * txt_encoding) {
8374 xmlDocDumpFormatMemoryEnc(out_doc, doc_txt_ptr, doc_txt_len,
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008375 txt_encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008376}
8377
8378/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008379 * xmlDocFormatDump:
Owen Taylor3473f882001-02-23 17:55:21 +00008380 * @f: the FILE*
8381 * @cur: the document
Daniel Veillard9e412302002-06-10 15:59:44 +00008382 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008383 *
8384 * Dump an XML document to an open FILE.
8385 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008386 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008387 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8388 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008389 */
8390int
Daniel Veillard9e412302002-06-10 15:59:44 +00008391xmlDocFormatDump(FILE *f, xmlDocPtr cur, int format) {
Owen Taylor3473f882001-02-23 17:55:21 +00008392 xmlOutputBufferPtr buf;
8393 const char * encoding;
8394 xmlCharEncodingHandlerPtr handler = NULL;
8395 int ret;
8396
8397 if (cur == NULL) {
8398#ifdef DEBUG_TREE
8399 xmlGenericError(xmlGenericErrorContext,
8400 "xmlDocDump : document == NULL\n");
8401#endif
8402 return(-1);
8403 }
8404 encoding = (const char *) cur->encoding;
8405
8406 if (encoding != NULL) {
Daniel Veillarde1326112003-06-05 09:32:20 +00008407 handler = xmlFindCharEncodingHandler(encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00008408 if (handler == NULL) {
8409 xmlFree((char *) cur->encoding);
8410 cur->encoding = NULL;
8411 }
8412 }
Owen Taylor3473f882001-02-23 17:55:21 +00008413 buf = xmlOutputBufferCreateFile(f, handler);
8414 if (buf == NULL) return(-1);
Daniel Veillard9e412302002-06-10 15:59:44 +00008415 xmlDocContentDumpOutput(buf, cur, NULL, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008416
8417 ret = xmlOutputBufferClose(buf);
8418 return(ret);
8419}
8420
8421/**
Daniel Veillard9e412302002-06-10 15:59:44 +00008422 * xmlDocDump:
8423 * @f: the FILE*
8424 * @cur: the document
8425 *
8426 * Dump an XML document to an open FILE.
8427 *
8428 * returns: the number of bytes written or -1 in case of failure.
8429 */
8430int
8431xmlDocDump(FILE *f, xmlDocPtr cur) {
Daniel Veillard828ce832003-10-08 19:19:10 +00008432 return(xmlDocFormatDump (f, cur, 0));
Daniel Veillard9e412302002-06-10 15:59:44 +00008433}
8434
8435/**
Owen Taylor3473f882001-02-23 17:55:21 +00008436 * xmlSaveFileTo:
8437 * @buf: an output I/O buffer
8438 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008439 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Owen Taylor3473f882001-02-23 17:55:21 +00008440 *
8441 * Dump an XML document to an I/O buffer.
8442 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008443 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008444 */
8445int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008446xmlSaveFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding) {
Owen Taylor3473f882001-02-23 17:55:21 +00008447 int ret;
8448
8449 if (buf == NULL) return(0);
Daniel Veillard1731d6a2001-04-10 16:38:06 +00008450 xmlDocContentDumpOutput(buf, cur, encoding, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008451 ret = xmlOutputBufferClose(buf);
8452 return(ret);
8453}
8454
8455/**
Daniel Veillardeefd4492001-04-28 16:55:50 +00008456 * xmlSaveFormatFileTo:
8457 * @buf: an output I/O buffer
8458 * @cur: the document
Daniel Veillardd1640922001-12-17 15:30:10 +00008459 * @encoding: the encoding if any assuming the I/O layer handles the trancoding
Daniel Veillardeefd4492001-04-28 16:55:50 +00008460 * @format: should formatting spaces been added
8461 *
8462 * Dump an XML document to an I/O buffer.
8463 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008464 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008465 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8466 * or xmlKeepBlanksDefault(0) was called
Daniel Veillardeefd4492001-04-28 16:55:50 +00008467 */
8468int
CET 2001 Daniel Veillard5a37bde2001-11-01 14:31:22 +00008469xmlSaveFormatFileTo(xmlOutputBufferPtr buf, xmlDocPtr cur, const char *encoding, int format) {
Daniel Veillardeefd4492001-04-28 16:55:50 +00008470 int ret;
8471
8472 if (buf == NULL) return(0);
8473 xmlDocContentDumpOutput(buf, cur, encoding, format);
8474 ret = xmlOutputBufferClose(buf);
8475 return(ret);
8476}
8477
8478/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00008479 * xmlSaveFormatFileEnc:
Daniel Veillardf012a642001-07-23 19:10:52 +00008480 * @filename: the filename or URL to output
8481 * @cur: the document being saved
8482 * @encoding: the name of the encoding to use or NULL.
8483 * @format: should formatting spaces be added.
Daniel Veillardd1640922001-12-17 15:30:10 +00008484 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00008485 * Dump an XML document to a file or an URL.
8486 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008487 * Returns the number of bytes written or -1 in case of error.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008488 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8489 * or xmlKeepBlanksDefault(0) was called
Owen Taylor3473f882001-02-23 17:55:21 +00008490 */
8491int
Daniel Veillardf012a642001-07-23 19:10:52 +00008492xmlSaveFormatFileEnc( const char * filename, xmlDocPtr cur,
8493 const char * encoding, int format ) {
Owen Taylor3473f882001-02-23 17:55:21 +00008494 xmlOutputBufferPtr buf;
8495 xmlCharEncodingHandlerPtr handler = NULL;
8496 int ret;
8497
Daniel Veillard4dbe77a2003-01-14 00:17:42 +00008498 if (cur == NULL)
8499 return(-1);
8500
Daniel Veillardfb25a512002-01-13 20:32:08 +00008501 if (encoding == NULL)
8502 encoding = (const char *) cur->encoding;
8503
Owen Taylor3473f882001-02-23 17:55:21 +00008504 if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008505
Owen Taylor3473f882001-02-23 17:55:21 +00008506 handler = xmlFindCharEncodingHandler(encoding);
Daniel Veillard81418e32001-05-22 15:08:55 +00008507 if (handler == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00008508 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00008509 }
8510
Daniel Veillardf012a642001-07-23 19:10:52 +00008511#ifdef HAVE_ZLIB_H
8512 if (cur->compression < 0) cur->compression = xmlCompressMode;
8513#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008514 /*
8515 * save the content to a temp buffer.
8516 */
Daniel Veillardf012a642001-07-23 19:10:52 +00008517 buf = xmlOutputBufferCreateFilename(filename, handler, cur->compression);
Owen Taylor3473f882001-02-23 17:55:21 +00008518 if (buf == NULL) return(-1);
8519
Daniel Veillardf012a642001-07-23 19:10:52 +00008520 xmlDocContentDumpOutput(buf, cur, encoding, format);
Owen Taylor3473f882001-02-23 17:55:21 +00008521
8522 ret = xmlOutputBufferClose(buf);
8523 return(ret);
8524}
8525
Daniel Veillardf012a642001-07-23 19:10:52 +00008526
8527/**
8528 * xmlSaveFileEnc:
8529 * @filename: the filename (or URL)
8530 * @cur: the document
8531 * @encoding: the name of an encoding (or NULL)
8532 *
8533 * Dump an XML document, converting it to the given encoding
8534 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008535 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillardf012a642001-07-23 19:10:52 +00008536 */
8537int
8538xmlSaveFileEnc(const char *filename, xmlDocPtr cur, const char *encoding) {
8539 return ( xmlSaveFormatFileEnc( filename, cur, encoding, 0 ) );
8540}
8541
Owen Taylor3473f882001-02-23 17:55:21 +00008542/**
Daniel Veillard67fee942001-04-26 18:59:03 +00008543 * xmlSaveFormatFile:
Owen Taylor3473f882001-02-23 17:55:21 +00008544 * @filename: the filename (or URL)
8545 * @cur: the document
Daniel Veillard67fee942001-04-26 18:59:03 +00008546 * @format: should formatting spaces been added
Owen Taylor3473f882001-02-23 17:55:21 +00008547 *
8548 * Dump an XML document to a file. Will use compression if
8549 * compiled in and enabled. If @filename is "-" the stdout file is
Daniel Veillardd1640922001-12-17 15:30:10 +00008550 * used. If @format is set then the document will be indented on output.
Daniel Veillard7424eb62003-01-24 14:14:52 +00008551 * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1
8552 * or xmlKeepBlanksDefault(0) was called
Daniel Veillard67fee942001-04-26 18:59:03 +00008553 *
Daniel Veillardd1640922001-12-17 15:30:10 +00008554 * returns: the number of bytes written or -1 in case of failure.
Owen Taylor3473f882001-02-23 17:55:21 +00008555 */
8556int
Daniel Veillard67fee942001-04-26 18:59:03 +00008557xmlSaveFormatFile(const char *filename, xmlDocPtr cur, int format) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008558 return ( xmlSaveFormatFileEnc( filename, cur, NULL, format ) );
Owen Taylor3473f882001-02-23 17:55:21 +00008559}
8560
Daniel Veillard67fee942001-04-26 18:59:03 +00008561/**
8562 * xmlSaveFile:
8563 * @filename: the filename (or URL)
8564 * @cur: the document
8565 *
8566 * Dump an XML document to a file. Will use compression if
8567 * compiled in and enabled. If @filename is "-" the stdout file is
8568 * used.
Daniel Veillardd1640922001-12-17 15:30:10 +00008569 * returns: the number of bytes written or -1 in case of failure.
Daniel Veillard67fee942001-04-26 18:59:03 +00008570 */
8571int
8572xmlSaveFile(const char *filename, xmlDocPtr cur) {
Daniel Veillardf012a642001-07-23 19:10:52 +00008573 return(xmlSaveFormatFileEnc(filename, cur, NULL, 0));
Daniel Veillard67fee942001-04-26 18:59:03 +00008574}
8575
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00008576#endif /* LIBXML_OUTPUT_ENABLED */
8577
8578/**
8579 * xmlGetDocCompressMode:
8580 * @doc: the document
8581 *
8582 * get the compression ratio for a document, ZLIB based
8583 * Returns 0 (uncompressed) to 9 (max compression)
8584 */
8585int
8586xmlGetDocCompressMode (xmlDocPtr doc) {
8587 if (doc == NULL) return(-1);
8588 return(doc->compression);
8589}
8590
8591/**
8592 * xmlSetDocCompressMode:
8593 * @doc: the document
8594 * @mode: the compression ratio
8595 *
8596 * set the compression ratio for a document, ZLIB based
8597 * Correct values: 0 (uncompressed) to 9 (max compression)
8598 */
8599void
8600xmlSetDocCompressMode (xmlDocPtr doc, int mode) {
8601 if (doc == NULL) return;
8602 if (mode < 0) doc->compression = 0;
8603 else if (mode > 9) doc->compression = 9;
8604 else doc->compression = mode;
8605}
8606
8607/**
8608 * xmlGetCompressMode:
8609 *
8610 * get the default compression mode used, ZLIB based.
8611 * Returns 0 (uncompressed) to 9 (max compression)
8612 */
8613int
8614xmlGetCompressMode(void)
8615{
8616 return (xmlCompressMode);
8617}
8618
8619/**
8620 * xmlSetCompressMode:
8621 * @mode: the compression ratio
8622 *
8623 * set the default compression mode used, ZLIB based
8624 * Correct values: 0 (uncompressed) to 9 (max compression)
8625 */
8626void
8627xmlSetCompressMode(int mode) {
8628 if (mode < 0) xmlCompressMode = 0;
8629 else if (mode > 9) xmlCompressMode = 9;
8630 else xmlCompressMode = mode;
8631}
8632