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