Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * tree.c : implementation of access function for an XML tree. |
3 | | * |
4 | | * References: |
5 | | * XHTML 1.0 W3C REC: http://www.w3.org/TR/2002/REC-xhtml1-20020801/ |
6 | | * |
7 | | * See Copyright for the status of this software. |
8 | | * |
9 | | * daniel@veillard.com |
10 | | * |
11 | | */ |
12 | | |
13 | | /* To avoid EBCDIC trouble when parsing on zOS */ |
14 | | #if defined(__MVS__) |
15 | | #pragma convert("ISO8859-1") |
16 | | #endif |
17 | | |
18 | | #define IN_LIBXML |
19 | | #include "libxml.h" |
20 | | |
21 | | #include <string.h> /* for memset() only ! */ |
22 | | #include <stddef.h> |
23 | | #include <limits.h> |
24 | | #include <ctype.h> |
25 | | #include <stdlib.h> |
26 | | |
27 | | #ifdef LIBXML_ZLIB_ENABLED |
28 | | #include <zlib.h> |
29 | | #endif |
30 | | |
31 | | #include <libxml/tree.h> |
32 | | #include <libxml/xmlmemory.h> |
33 | | #include <libxml/parser.h> |
34 | | #include <libxml/uri.h> |
35 | | #include <libxml/entities.h> |
36 | | #include <libxml/xmlerror.h> |
37 | | #include <libxml/parserInternals.h> |
38 | | #ifdef LIBXML_HTML_ENABLED |
39 | | #include <libxml/HTMLtree.h> |
40 | | #endif |
41 | | #ifdef LIBXML_DEBUG_ENABLED |
42 | | #include <libxml/debugXML.h> |
43 | | #endif |
44 | | |
45 | | #include "private/buf.h" |
46 | | #include "private/entities.h" |
47 | | #include "private/error.h" |
48 | | #include "private/tree.h" |
49 | | |
50 | | /* |
51 | | * Internal variable indicating whether a callback has been registered |
52 | | * for node creation/destruction. This avoids looking up thread-local |
53 | | * data if no callback was ever registered. |
54 | | */ |
55 | | int xmlRegisterCallbacks = 0; |
56 | | |
57 | | /************************************************************************ |
58 | | * * |
59 | | * Forward declarations * |
60 | | * * |
61 | | ************************************************************************/ |
62 | | |
63 | | static xmlNodePtr |
64 | | xmlNewEntityRef(xmlDocPtr doc, xmlChar *name); |
65 | | |
66 | | static xmlNsPtr |
67 | | xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns); |
68 | | |
69 | | static xmlAttrPtr |
70 | | xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name, |
71 | | const xmlChar *nsName, int useDTD); |
72 | | |
73 | | static xmlChar* xmlGetPropNodeValueInternal(const xmlAttr *prop); |
74 | | |
75 | | static void |
76 | | xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree); |
77 | | |
78 | | static void |
79 | | xmlUnlinkNodeInternal(xmlNodePtr cur); |
80 | | |
81 | | /************************************************************************ |
82 | | * * |
83 | | * A few static variables and macros * |
84 | | * * |
85 | | ************************************************************************/ |
86 | | /* #undef xmlStringText */ |
87 | | const xmlChar xmlStringText[] = { 't', 'e', 'x', 't', 0 }; |
88 | | /* #undef xmlStringTextNoenc */ |
89 | | const xmlChar xmlStringTextNoenc[] = |
90 | | { 't', 'e', 'x', 't', 'n', 'o', 'e', 'n', 'c', 0 }; |
91 | | /* #undef xmlStringComment */ |
92 | | const xmlChar xmlStringComment[] = { 'c', 'o', 'm', 'm', 'e', 'n', 't', 0 }; |
93 | | |
94 | | static int xmlCompressMode = 0; |
95 | | |
96 | 8.13k | #define IS_STR_XML(str) ((str != NULL) && (str[0] == 'x') && \ |
97 | 8.13k | (str[1] == 'm') && (str[2] == 'l') && (str[3] == 0)) |
98 | | |
99 | | /************************************************************************ |
100 | | * * |
101 | | * Functions to move to entities.c once the * |
102 | | * API freeze is smoothen and they can be made public. * |
103 | | * * |
104 | | ************************************************************************/ |
105 | | #include <libxml/hash.h> |
106 | | |
107 | | /** |
108 | | * xmlGetEntityFromDtd: |
109 | | * @dtd: A pointer to the DTD to search |
110 | | * @name: The entity name |
111 | | * |
112 | | * Do an entity lookup in the DTD entity hash table and |
113 | | * return the corresponding entity, if found. |
114 | | * |
115 | | * Returns A pointer to the entity structure or NULL if not found. |
116 | | */ |
117 | | static xmlEntityPtr |
118 | 0 | xmlGetEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
119 | 0 | xmlEntitiesTablePtr table; |
120 | |
|
121 | 0 | if((dtd != NULL) && (dtd->entities != NULL)) { |
122 | 0 | table = (xmlEntitiesTablePtr) dtd->entities; |
123 | 0 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
124 | | /* return(xmlGetEntityFromTable(table, name)); */ |
125 | 0 | } |
126 | 0 | return(NULL); |
127 | 0 | } |
128 | | /** |
129 | | * xmlGetParameterEntityFromDtd: |
130 | | * @dtd: A pointer to the DTD to search |
131 | | * @name: The entity name |
132 | | * |
133 | | * Do an entity lookup in the DTD parameter entity hash table and |
134 | | * return the corresponding entity, if found. |
135 | | * |
136 | | * Returns A pointer to the entity structure or NULL if not found. |
137 | | */ |
138 | | static xmlEntityPtr |
139 | 0 | xmlGetParameterEntityFromDtd(const xmlDtd *dtd, const xmlChar *name) { |
140 | 0 | xmlEntitiesTablePtr table; |
141 | |
|
142 | 0 | if ((dtd != NULL) && (dtd->pentities != NULL)) { |
143 | 0 | table = (xmlEntitiesTablePtr) dtd->pentities; |
144 | 0 | return((xmlEntityPtr) xmlHashLookup(table, name)); |
145 | | /* return(xmlGetEntityFromTable(table, name)); */ |
146 | 0 | } |
147 | 0 | return(NULL); |
148 | 0 | } |
149 | | |
150 | | /************************************************************************ |
151 | | * * |
152 | | * QName handling helper * |
153 | | * * |
154 | | ************************************************************************/ |
155 | | |
156 | | /** |
157 | | * xmlBuildQName: |
158 | | * @ncname: the Name |
159 | | * @prefix: the prefix |
160 | | * @memory: preallocated memory |
161 | | * @len: preallocated memory length |
162 | | * |
163 | | * Builds the QName @prefix:@ncname in @memory if there is enough space |
164 | | * and prefix is not NULL nor empty, otherwise allocate a new string. |
165 | | * If prefix is NULL or empty it returns ncname. |
166 | | * |
167 | | * Returns the new string which must be freed by the caller if different from |
168 | | * @memory and @ncname or NULL in case of error |
169 | | */ |
170 | | xmlChar * |
171 | | xmlBuildQName(const xmlChar *ncname, const xmlChar *prefix, |
172 | 63 | xmlChar *memory, int len) { |
173 | 63 | int lenn, lenp; |
174 | 63 | xmlChar *ret; |
175 | | |
176 | 63 | if (ncname == NULL) return(NULL); |
177 | 63 | if (prefix == NULL) return((xmlChar *) ncname); |
178 | | |
179 | 63 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
180 | | /* Make allocation more likely */ |
181 | 63 | if (len > 8) |
182 | 63 | len = 8; |
183 | 63 | #endif |
184 | | |
185 | 63 | lenn = strlen((char *) ncname); |
186 | 63 | lenp = strlen((char *) prefix); |
187 | | |
188 | 63 | if ((memory == NULL) || (len < lenn + lenp + 2)) { |
189 | 34 | ret = xmlMalloc(lenn + lenp + 2); |
190 | 34 | if (ret == NULL) |
191 | 0 | return(NULL); |
192 | 34 | } else { |
193 | 29 | ret = memory; |
194 | 29 | } |
195 | 63 | memcpy(&ret[0], prefix, lenp); |
196 | 63 | ret[lenp] = ':'; |
197 | 63 | memcpy(&ret[lenp + 1], ncname, lenn); |
198 | 63 | ret[lenn + lenp + 1] = 0; |
199 | 63 | return(ret); |
200 | 63 | } |
201 | | |
202 | | /** |
203 | | * xmlSplitQName2: |
204 | | * @name: the full QName |
205 | | * @prefix: a xmlChar ** |
206 | | * |
207 | | * DEPRECATED: This function doesn't report malloc failures. |
208 | | * |
209 | | * parse an XML qualified name string |
210 | | * |
211 | | * [NS 5] QName ::= (Prefix ':')? LocalPart |
212 | | * |
213 | | * [NS 6] Prefix ::= NCName |
214 | | * |
215 | | * [NS 7] LocalPart ::= NCName |
216 | | * |
217 | | * Returns NULL if the name doesn't have a prefix. Otherwise, returns the |
218 | | * local part, and prefix is updated to get the Prefix. Both the return value |
219 | | * and the prefix must be freed by the caller. |
220 | | */ |
221 | | xmlChar * |
222 | 0 | xmlSplitQName2(const xmlChar *name, xmlChar **prefix) { |
223 | 0 | int len = 0; |
224 | 0 | xmlChar *ret = NULL; |
225 | |
|
226 | 0 | if (prefix == NULL) return(NULL); |
227 | 0 | *prefix = NULL; |
228 | 0 | if (name == NULL) return(NULL); |
229 | | |
230 | | /* nasty but valid */ |
231 | 0 | if (name[0] == ':') |
232 | 0 | return(NULL); |
233 | | |
234 | | /* |
235 | | * we are not trying to validate but just to cut, and yes it will |
236 | | * work even if this is as set of UTF-8 encoded chars |
237 | | */ |
238 | 0 | while ((name[len] != 0) && (name[len] != ':')) |
239 | 0 | len++; |
240 | |
|
241 | 0 | if ((name[len] == 0) || (name[len+1] == 0)) |
242 | 0 | return(NULL); |
243 | | |
244 | 0 | *prefix = xmlStrndup(name, len); |
245 | 0 | if (*prefix == NULL) |
246 | 0 | return(NULL); |
247 | 0 | ret = xmlStrdup(&name[len + 1]); |
248 | 0 | if (ret == NULL) { |
249 | 0 | if (*prefix != NULL) { |
250 | 0 | xmlFree(*prefix); |
251 | 0 | *prefix = NULL; |
252 | 0 | } |
253 | 0 | return(NULL); |
254 | 0 | } |
255 | | |
256 | 0 | return(ret); |
257 | 0 | } |
258 | | |
259 | | /** |
260 | | * xmlSplitQName3: |
261 | | * @name: the full QName |
262 | | * @len: an int * |
263 | | * |
264 | | * parse an XML qualified name string,i |
265 | | * |
266 | | * returns NULL if it is not a Qualified Name, otherwise, update len |
267 | | * with the length in byte of the prefix and return a pointer |
268 | | * to the start of the name without the prefix |
269 | | */ |
270 | | |
271 | | const xmlChar * |
272 | 64.8k | xmlSplitQName3(const xmlChar *name, int *len) { |
273 | 64.8k | int l = 0; |
274 | | |
275 | 64.8k | if (name == NULL) return(NULL); |
276 | 64.8k | if (len == NULL) return(NULL); |
277 | | |
278 | | /* nasty but valid */ |
279 | 64.8k | if (name[0] == ':') |
280 | 3.97k | return(NULL); |
281 | | |
282 | | /* |
283 | | * we are not trying to validate but just to cut, and yes it will |
284 | | * work even if this is as set of UTF-8 encoded chars |
285 | | */ |
286 | 4.72M | while ((name[l] != 0) && (name[l] != ':')) |
287 | 4.66M | l++; |
288 | | |
289 | 60.8k | if ((name[l] == 0) || (name[l+1] == 0)) |
290 | 46.2k | return(NULL); |
291 | | |
292 | 14.6k | *len = l; |
293 | | |
294 | 14.6k | return(&name[l+1]); |
295 | 60.8k | } |
296 | | |
297 | | /** |
298 | | * xmlSplitQName4: |
299 | | * @name: the full QName |
300 | | * @prefixPtr: pointer to resulting prefix |
301 | | * |
302 | | * Parse a QName. The return value points to the start of the local |
303 | | * name in the input string. If the QName has a prefix, it will be |
304 | | * allocated and stored in @prefixPtr. This string must be freed by |
305 | | * the caller. If there's no prefix, @prefixPtr is set to NULL. |
306 | | * |
307 | | * Returns the local name or NULL if a memory allocation failed. |
308 | | */ |
309 | | const xmlChar * |
310 | 79.1k | xmlSplitQName4(const xmlChar *name, xmlChar **prefixPtr) { |
311 | 79.1k | xmlChar *prefix; |
312 | 79.1k | int l = 0; |
313 | | |
314 | 79.1k | if ((name == NULL) || (prefixPtr == NULL)) |
315 | 0 | return(NULL); |
316 | | |
317 | 79.1k | *prefixPtr = NULL; |
318 | | |
319 | | /* nasty but valid */ |
320 | 79.1k | if (name[0] == ':') |
321 | 7.36k | return(name); |
322 | | |
323 | | /* |
324 | | * we are not trying to validate but just to cut, and yes it will |
325 | | * work even if this is as set of UTF-8 encoded chars |
326 | | */ |
327 | 6.32M | while ((name[l] != 0) && (name[l] != ':')) |
328 | 6.25M | l++; |
329 | | |
330 | | /* |
331 | | * TODO: What about names with multiple colons? |
332 | | */ |
333 | 71.7k | if ((name[l] == 0) || (name[l+1] == 0)) |
334 | 54.9k | return(name); |
335 | | |
336 | 16.7k | prefix = xmlStrndup(name, l); |
337 | 16.7k | if (prefix == NULL) |
338 | 0 | return(NULL); |
339 | | |
340 | 16.7k | *prefixPtr = prefix; |
341 | 16.7k | return(&name[l+1]); |
342 | 16.7k | } |
343 | | |
344 | | /************************************************************************ |
345 | | * * |
346 | | * Check Name, NCName and QName strings * |
347 | | * * |
348 | | ************************************************************************/ |
349 | | |
350 | 195k | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
351 | | |
352 | | /** |
353 | | * xmlValidateNCName: |
354 | | * @value: the value to check |
355 | | * @space: allow spaces in front and end of the string |
356 | | * |
357 | | * Check that a value conforms to the lexical space of NCName |
358 | | * |
359 | | * Returns 0 if this validates, a positive error code number otherwise |
360 | | * and -1 in case of internal or API error. |
361 | | */ |
362 | | int |
363 | 7.09k | xmlValidateNCName(const xmlChar *value, int space) { |
364 | 7.09k | const xmlChar *cur = value; |
365 | 7.09k | int c,l; |
366 | | |
367 | 7.09k | if (value == NULL) |
368 | 0 | return(-1); |
369 | | |
370 | | /* |
371 | | * First quick algorithm for ASCII range |
372 | | */ |
373 | 7.09k | if (space) |
374 | 63.9k | while (IS_BLANK_CH(*cur)) cur++; |
375 | 7.09k | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
376 | 7.09k | (*cur == '_')) |
377 | 4.72k | cur++; |
378 | 2.37k | else |
379 | 2.37k | goto try_complex; |
380 | 31.5k | while (((*cur >= 'a') && (*cur <= 'z')) || |
381 | 31.5k | ((*cur >= 'A') && (*cur <= 'Z')) || |
382 | 31.5k | ((*cur >= '0') && (*cur <= '9')) || |
383 | 31.5k | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
384 | 26.8k | cur++; |
385 | 4.72k | if (space) |
386 | 69.1k | while (IS_BLANK_CH(*cur)) cur++; |
387 | 4.72k | if (*cur == 0) |
388 | 796 | return(0); |
389 | | |
390 | 6.30k | try_complex: |
391 | | /* |
392 | | * Second check for chars outside the ASCII range |
393 | | */ |
394 | 6.30k | cur = value; |
395 | 6.30k | c = CUR_SCHAR(cur, l); |
396 | 6.30k | if (space) { |
397 | 63.8k | while (IS_BLANK(c)) { |
398 | 63.8k | cur += l; |
399 | 63.8k | c = CUR_SCHAR(cur, l); |
400 | 63.8k | } |
401 | 6.30k | } |
402 | 6.30k | if ((!IS_LETTER(c)) && (c != '_')) |
403 | 988 | return(1); |
404 | 5.31k | cur += l; |
405 | 5.31k | c = CUR_SCHAR(cur, l); |
406 | 39.6k | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
407 | 39.6k | (c == '-') || (c == '_') || IS_COMBINING(c) || |
408 | 39.6k | IS_EXTENDER(c)) { |
409 | 34.3k | cur += l; |
410 | 34.3k | c = CUR_SCHAR(cur, l); |
411 | 34.3k | } |
412 | 5.31k | if (space) { |
413 | 85.3k | while (IS_BLANK(c)) { |
414 | 85.3k | cur += l; |
415 | 85.3k | c = CUR_SCHAR(cur, l); |
416 | 85.3k | } |
417 | 5.31k | } |
418 | 5.31k | if (c != 0) |
419 | 4.40k | return(1); |
420 | | |
421 | 910 | return(0); |
422 | 5.31k | } |
423 | | |
424 | | /** |
425 | | * xmlValidateQName: |
426 | | * @value: the value to check |
427 | | * @space: allow spaces in front and end of the string |
428 | | * |
429 | | * Check that a value conforms to the lexical space of QName |
430 | | * |
431 | | * Returns 0 if this validates, a positive error code number otherwise |
432 | | * and -1 in case of internal or API error. |
433 | | */ |
434 | | int |
435 | 0 | xmlValidateQName(const xmlChar *value, int space) { |
436 | 0 | const xmlChar *cur = value; |
437 | 0 | int c,l; |
438 | |
|
439 | 0 | if (value == NULL) |
440 | 0 | return(-1); |
441 | | /* |
442 | | * First quick algorithm for ASCII range |
443 | | */ |
444 | 0 | if (space) |
445 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
446 | 0 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
447 | 0 | (*cur == '_')) |
448 | 0 | cur++; |
449 | 0 | else |
450 | 0 | goto try_complex; |
451 | 0 | while (((*cur >= 'a') && (*cur <= 'z')) || |
452 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
453 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
454 | 0 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
455 | 0 | cur++; |
456 | 0 | if (*cur == ':') { |
457 | 0 | cur++; |
458 | 0 | if (((*cur >= 'a') && (*cur <= 'z')) || |
459 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
460 | 0 | (*cur == '_')) |
461 | 0 | cur++; |
462 | 0 | else |
463 | 0 | goto try_complex; |
464 | 0 | while (((*cur >= 'a') && (*cur <= 'z')) || |
465 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
466 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
467 | 0 | (*cur == '_') || (*cur == '-') || (*cur == '.')) |
468 | 0 | cur++; |
469 | 0 | } |
470 | 0 | if (space) |
471 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
472 | 0 | if (*cur == 0) |
473 | 0 | return(0); |
474 | | |
475 | 0 | try_complex: |
476 | | /* |
477 | | * Second check for chars outside the ASCII range |
478 | | */ |
479 | 0 | cur = value; |
480 | 0 | c = CUR_SCHAR(cur, l); |
481 | 0 | if (space) { |
482 | 0 | while (IS_BLANK(c)) { |
483 | 0 | cur += l; |
484 | 0 | c = CUR_SCHAR(cur, l); |
485 | 0 | } |
486 | 0 | } |
487 | 0 | if ((!IS_LETTER(c)) && (c != '_')) |
488 | 0 | return(1); |
489 | 0 | cur += l; |
490 | 0 | c = CUR_SCHAR(cur, l); |
491 | 0 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
492 | 0 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
493 | 0 | IS_EXTENDER(c)) { |
494 | 0 | cur += l; |
495 | 0 | c = CUR_SCHAR(cur, l); |
496 | 0 | } |
497 | 0 | if (c == ':') { |
498 | 0 | cur += l; |
499 | 0 | c = CUR_SCHAR(cur, l); |
500 | 0 | if ((!IS_LETTER(c)) && (c != '_')) |
501 | 0 | return(1); |
502 | 0 | cur += l; |
503 | 0 | c = CUR_SCHAR(cur, l); |
504 | 0 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || |
505 | 0 | (c == '-') || (c == '_') || IS_COMBINING(c) || |
506 | 0 | IS_EXTENDER(c)) { |
507 | 0 | cur += l; |
508 | 0 | c = CUR_SCHAR(cur, l); |
509 | 0 | } |
510 | 0 | } |
511 | 0 | if (space) { |
512 | 0 | while (IS_BLANK(c)) { |
513 | 0 | cur += l; |
514 | 0 | c = CUR_SCHAR(cur, l); |
515 | 0 | } |
516 | 0 | } |
517 | 0 | if (c != 0) |
518 | 0 | return(1); |
519 | 0 | return(0); |
520 | 0 | } |
521 | | |
522 | | /** |
523 | | * xmlValidateName: |
524 | | * @value: the value to check |
525 | | * @space: allow spaces in front and end of the string |
526 | | * |
527 | | * Check that a value conforms to the lexical space of Name |
528 | | * |
529 | | * Returns 0 if this validates, a positive error code number otherwise |
530 | | * and -1 in case of internal or API error. |
531 | | */ |
532 | | int |
533 | 0 | xmlValidateName(const xmlChar *value, int space) { |
534 | 0 | const xmlChar *cur = value; |
535 | 0 | int c,l; |
536 | |
|
537 | 0 | if (value == NULL) |
538 | 0 | return(-1); |
539 | | /* |
540 | | * First quick algorithm for ASCII range |
541 | | */ |
542 | 0 | if (space) |
543 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
544 | 0 | if (((*cur >= 'a') && (*cur <= 'z')) || ((*cur >= 'A') && (*cur <= 'Z')) || |
545 | 0 | (*cur == '_') || (*cur == ':')) |
546 | 0 | cur++; |
547 | 0 | else |
548 | 0 | goto try_complex; |
549 | 0 | while (((*cur >= 'a') && (*cur <= 'z')) || |
550 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
551 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
552 | 0 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
553 | 0 | cur++; |
554 | 0 | if (space) |
555 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
556 | 0 | if (*cur == 0) |
557 | 0 | return(0); |
558 | | |
559 | 0 | try_complex: |
560 | | /* |
561 | | * Second check for chars outside the ASCII range |
562 | | */ |
563 | 0 | cur = value; |
564 | 0 | c = CUR_SCHAR(cur, l); |
565 | 0 | if (space) { |
566 | 0 | while (IS_BLANK(c)) { |
567 | 0 | cur += l; |
568 | 0 | c = CUR_SCHAR(cur, l); |
569 | 0 | } |
570 | 0 | } |
571 | 0 | if ((!IS_LETTER(c)) && (c != '_') && (c != ':')) |
572 | 0 | return(1); |
573 | 0 | cur += l; |
574 | 0 | c = CUR_SCHAR(cur, l); |
575 | 0 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
576 | 0 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
577 | 0 | cur += l; |
578 | 0 | c = CUR_SCHAR(cur, l); |
579 | 0 | } |
580 | 0 | if (space) { |
581 | 0 | while (IS_BLANK(c)) { |
582 | 0 | cur += l; |
583 | 0 | c = CUR_SCHAR(cur, l); |
584 | 0 | } |
585 | 0 | } |
586 | 0 | if (c != 0) |
587 | 0 | return(1); |
588 | 0 | return(0); |
589 | 0 | } |
590 | | |
591 | | /** |
592 | | * xmlValidateNMToken: |
593 | | * @value: the value to check |
594 | | * @space: allow spaces in front and end of the string |
595 | | * |
596 | | * Check that a value conforms to the lexical space of NMToken |
597 | | * |
598 | | * Returns 0 if this validates, a positive error code number otherwise |
599 | | * and -1 in case of internal or API error. |
600 | | */ |
601 | | int |
602 | 0 | xmlValidateNMToken(const xmlChar *value, int space) { |
603 | 0 | const xmlChar *cur = value; |
604 | 0 | int c,l; |
605 | |
|
606 | 0 | if (value == NULL) |
607 | 0 | return(-1); |
608 | | /* |
609 | | * First quick algorithm for ASCII range |
610 | | */ |
611 | 0 | if (space) |
612 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
613 | 0 | if (((*cur >= 'a') && (*cur <= 'z')) || |
614 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
615 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
616 | 0 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
617 | 0 | cur++; |
618 | 0 | else |
619 | 0 | goto try_complex; |
620 | 0 | while (((*cur >= 'a') && (*cur <= 'z')) || |
621 | 0 | ((*cur >= 'A') && (*cur <= 'Z')) || |
622 | 0 | ((*cur >= '0') && (*cur <= '9')) || |
623 | 0 | (*cur == '_') || (*cur == '-') || (*cur == '.') || (*cur == ':')) |
624 | 0 | cur++; |
625 | 0 | if (space) |
626 | 0 | while (IS_BLANK_CH(*cur)) cur++; |
627 | 0 | if (*cur == 0) |
628 | 0 | return(0); |
629 | | |
630 | 0 | try_complex: |
631 | | /* |
632 | | * Second check for chars outside the ASCII range |
633 | | */ |
634 | 0 | cur = value; |
635 | 0 | c = CUR_SCHAR(cur, l); |
636 | 0 | if (space) { |
637 | 0 | while (IS_BLANK(c)) { |
638 | 0 | cur += l; |
639 | 0 | c = CUR_SCHAR(cur, l); |
640 | 0 | } |
641 | 0 | } |
642 | 0 | if (!(IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
643 | 0 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c))) |
644 | 0 | return(1); |
645 | 0 | cur += l; |
646 | 0 | c = CUR_SCHAR(cur, l); |
647 | 0 | while (IS_LETTER(c) || IS_DIGIT(c) || (c == '.') || (c == ':') || |
648 | 0 | (c == '-') || (c == '_') || IS_COMBINING(c) || IS_EXTENDER(c)) { |
649 | 0 | cur += l; |
650 | 0 | c = CUR_SCHAR(cur, l); |
651 | 0 | } |
652 | 0 | if (space) { |
653 | 0 | while (IS_BLANK(c)) { |
654 | 0 | cur += l; |
655 | 0 | c = CUR_SCHAR(cur, l); |
656 | 0 | } |
657 | 0 | } |
658 | 0 | if (c != 0) |
659 | 0 | return(1); |
660 | 0 | return(0); |
661 | 0 | } |
662 | | |
663 | | /************************************************************************ |
664 | | * * |
665 | | * Allocation and deallocation of basic structures * |
666 | | * * |
667 | | ************************************************************************/ |
668 | | |
669 | | /** |
670 | | * xmlNewNs: |
671 | | * @node: the element carrying the namespace (optional) |
672 | | * @href: the URI associated |
673 | | * @prefix: the prefix for the namespace (optional) |
674 | | * |
675 | | * Create a new namespace. For a default namespace, @prefix should be |
676 | | * NULL. The namespace URI in @href is not checked. You should make sure |
677 | | * to pass a valid URI. |
678 | | * |
679 | | * If @node is provided, it must be an element node. The namespace will |
680 | | * be appended to the node's namespace declarations. It is an error if |
681 | | * the node already has a definition for the prefix or default |
682 | | * namespace. |
683 | | * |
684 | | * Returns a new namespace pointer or NULL if arguments are invalid, |
685 | | * the prefix is already in use or a memory allocation failed. |
686 | | */ |
687 | | xmlNsPtr |
688 | 8.37k | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
689 | 8.37k | xmlNsPtr cur; |
690 | | |
691 | 8.37k | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
692 | 0 | return(NULL); |
693 | | |
694 | | /* |
695 | | * Allocate a new Namespace and fill the fields. |
696 | | */ |
697 | 8.37k | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
698 | 8.37k | if (cur == NULL) |
699 | 0 | return(NULL); |
700 | 8.37k | memset(cur, 0, sizeof(xmlNs)); |
701 | 8.37k | cur->type = XML_LOCAL_NAMESPACE; |
702 | | |
703 | 8.37k | if (href != NULL) { |
704 | 8.37k | cur->href = xmlStrdup(href); |
705 | 8.37k | if (cur->href == NULL) |
706 | 0 | goto error; |
707 | 8.37k | } |
708 | 8.37k | if (prefix != NULL) { |
709 | 5.82k | cur->prefix = xmlStrdup(prefix); |
710 | 5.82k | if (cur->prefix == NULL) |
711 | 0 | goto error; |
712 | 5.82k | } |
713 | | |
714 | | /* |
715 | | * Add it at the end to preserve parsing order ... |
716 | | * and checks for existing use of the prefix |
717 | | */ |
718 | 8.37k | if (node != NULL) { |
719 | 0 | if (node->nsDef == NULL) { |
720 | 0 | node->nsDef = cur; |
721 | 0 | } else { |
722 | 0 | xmlNsPtr prev = node->nsDef; |
723 | |
|
724 | 0 | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
725 | 0 | (prev->href != NULL)) |
726 | 0 | goto error; |
727 | 0 | while (prev->next != NULL) { |
728 | 0 | prev = prev->next; |
729 | 0 | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
730 | 0 | (prev->href != NULL)) |
731 | 0 | goto error; |
732 | 0 | } |
733 | 0 | prev->next = cur; |
734 | 0 | } |
735 | 0 | } |
736 | 8.37k | return(cur); |
737 | | |
738 | 0 | error: |
739 | 0 | xmlFreeNs(cur); |
740 | 0 | return(NULL); |
741 | 8.37k | } |
742 | | |
743 | | /** |
744 | | * xmlSetNs: |
745 | | * @node: a node in the document |
746 | | * @ns: a namespace pointer (optional) |
747 | | * |
748 | | * Set the namespace of an element or attribute node. Passing a NULL |
749 | | * namespace unsets the namespace. |
750 | | */ |
751 | | void |
752 | 0 | xmlSetNs(xmlNodePtr node, xmlNsPtr ns) { |
753 | 0 | if (node == NULL) { |
754 | 0 | return; |
755 | 0 | } |
756 | 0 | if ((node->type == XML_ELEMENT_NODE) || |
757 | 0 | (node->type == XML_ATTRIBUTE_NODE)) |
758 | 0 | node->ns = ns; |
759 | 0 | } |
760 | | |
761 | | /** |
762 | | * xmlFreeNs: |
763 | | * @cur: the namespace pointer |
764 | | * |
765 | | * Free an xmlNs object. |
766 | | */ |
767 | | void |
768 | 9.14k | xmlFreeNs(xmlNsPtr cur) { |
769 | 9.14k | if (cur == NULL) { |
770 | 0 | return; |
771 | 0 | } |
772 | 9.14k | if (cur->href != NULL) xmlFree((char *) cur->href); |
773 | 9.14k | if (cur->prefix != NULL) xmlFree((char *) cur->prefix); |
774 | 9.14k | xmlFree(cur); |
775 | 9.14k | } |
776 | | |
777 | | /** |
778 | | * xmlFreeNsList: |
779 | | * @cur: the first namespace pointer |
780 | | * |
781 | | * Free a list of xmlNs objects. |
782 | | */ |
783 | | void |
784 | 4.85k | xmlFreeNsList(xmlNsPtr cur) { |
785 | 4.85k | xmlNsPtr next; |
786 | 4.85k | if (cur == NULL) { |
787 | 0 | return; |
788 | 0 | } |
789 | 14.0k | while (cur != NULL) { |
790 | 9.14k | next = cur->next; |
791 | 9.14k | xmlFreeNs(cur); |
792 | 9.14k | cur = next; |
793 | 9.14k | } |
794 | 4.85k | } |
795 | | |
796 | | /** |
797 | | * xmlNewDtd: |
798 | | * @doc: the document pointer (optional) |
799 | | * @name: the DTD name (optional) |
800 | | * @ExternalID: the external ID (optional) |
801 | | * @SystemID: the system ID (optional) |
802 | | * |
803 | | * Create a DTD node. |
804 | | * |
805 | | * If a document is provided, it is an error if it already has an |
806 | | * external subset. If the document has no external subset, it |
807 | | * will be set to the created DTD. |
808 | | * |
809 | | * To create an internal subset, use xmlCreateIntSubset(). |
810 | | * |
811 | | * Returns a pointer to the new DTD object or NULL if arguments are |
812 | | * invalid or a memory allocation failed. |
813 | | */ |
814 | | xmlDtdPtr |
815 | | xmlNewDtd(xmlDocPtr doc, const xmlChar *name, |
816 | 469 | const xmlChar *ExternalID, const xmlChar *SystemID) { |
817 | 469 | xmlDtdPtr cur; |
818 | | |
819 | 469 | if ((doc != NULL) && (doc->extSubset != NULL)) { |
820 | 0 | return(NULL); |
821 | 0 | } |
822 | | |
823 | | /* |
824 | | * Allocate a new DTD and fill the fields. |
825 | | */ |
826 | 469 | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
827 | 469 | if (cur == NULL) |
828 | 0 | return(NULL); |
829 | 469 | memset(cur, 0 , sizeof(xmlDtd)); |
830 | 469 | cur->type = XML_DTD_NODE; |
831 | | |
832 | 469 | if (name != NULL) { |
833 | 469 | cur->name = xmlStrdup(name); |
834 | 469 | if (cur->name == NULL) |
835 | 0 | goto error; |
836 | 469 | } |
837 | 469 | if (ExternalID != NULL) { |
838 | 0 | cur->ExternalID = xmlStrdup(ExternalID); |
839 | 0 | if (cur->ExternalID == NULL) |
840 | 0 | goto error; |
841 | 0 | } |
842 | 469 | if (SystemID != NULL) { |
843 | 0 | cur->SystemID = xmlStrdup(SystemID); |
844 | 0 | if (cur->SystemID == NULL) |
845 | 0 | goto error; |
846 | 0 | } |
847 | 469 | if (doc != NULL) |
848 | 469 | doc->extSubset = cur; |
849 | 469 | cur->doc = doc; |
850 | | |
851 | 469 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
852 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
853 | 469 | return(cur); |
854 | | |
855 | 0 | error: |
856 | 0 | xmlFreeDtd(cur); |
857 | 0 | return(NULL); |
858 | 469 | } |
859 | | |
860 | | /** |
861 | | * xmlGetIntSubset: |
862 | | * @doc: the document pointer |
863 | | * |
864 | | * Get the internal subset of a document. |
865 | | * |
866 | | * Returns a pointer to the DTD object or NULL if not found. |
867 | | */ |
868 | | xmlDtdPtr |
869 | 11.5k | xmlGetIntSubset(const xmlDoc *doc) { |
870 | 11.5k | xmlNodePtr cur; |
871 | | |
872 | 11.5k | if (doc == NULL) |
873 | 0 | return(NULL); |
874 | 11.5k | cur = doc->children; |
875 | 12.3k | while (cur != NULL) { |
876 | 750 | if (cur->type == XML_DTD_NODE) |
877 | 0 | return((xmlDtdPtr) cur); |
878 | 750 | cur = cur->next; |
879 | 750 | } |
880 | 11.5k | return((xmlDtdPtr) doc->intSubset); |
881 | 11.5k | } |
882 | | |
883 | | /** |
884 | | * xmlCreateIntSubset: |
885 | | * @doc: the document pointer (optional) |
886 | | * @name: the DTD name (optional) |
887 | | * @ExternalID: the external (PUBLIC) ID (optional) |
888 | | * @SystemID: the system ID (optional) |
889 | | * |
890 | | * Create a DTD node. |
891 | | * |
892 | | * If a document is provided and it already has an internal subset, |
893 | | * the existing DTD object is returned without creating a new object. |
894 | | * If the document has no internal subset, it will be set to the |
895 | | * created DTD. |
896 | | * |
897 | | * Returns a pointer to the new or existing DTD object or NULL if |
898 | | * arguments are invalid or a memory allocation failed. |
899 | | */ |
900 | | xmlDtdPtr |
901 | | xmlCreateIntSubset(xmlDocPtr doc, const xmlChar *name, |
902 | 5.77k | const xmlChar *ExternalID, const xmlChar *SystemID) { |
903 | 5.77k | xmlDtdPtr cur; |
904 | | |
905 | 5.77k | if (doc != NULL) { |
906 | 5.77k | cur = xmlGetIntSubset(doc); |
907 | 5.77k | if (cur != NULL) |
908 | 0 | return(cur); |
909 | 5.77k | } |
910 | | |
911 | | /* |
912 | | * Allocate a new DTD and fill the fields. |
913 | | */ |
914 | 5.77k | cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd)); |
915 | 5.77k | if (cur == NULL) |
916 | 0 | return(NULL); |
917 | 5.77k | memset(cur, 0, sizeof(xmlDtd)); |
918 | 5.77k | cur->type = XML_DTD_NODE; |
919 | | |
920 | 5.77k | if (name != NULL) { |
921 | 5.77k | cur->name = xmlStrdup(name); |
922 | 5.77k | if (cur->name == NULL) |
923 | 0 | goto error; |
924 | 5.77k | } |
925 | 5.77k | if (ExternalID != NULL) { |
926 | 43 | cur->ExternalID = xmlStrdup(ExternalID); |
927 | 43 | if (cur->ExternalID == NULL) |
928 | 0 | goto error; |
929 | 43 | } |
930 | 5.77k | if (SystemID != NULL) { |
931 | 178 | cur->SystemID = xmlStrdup(SystemID); |
932 | 178 | if (cur->SystemID == NULL) |
933 | 0 | goto error; |
934 | 178 | } |
935 | 5.77k | if (doc != NULL) { |
936 | 5.77k | doc->intSubset = cur; |
937 | 5.77k | cur->parent = doc; |
938 | 5.77k | cur->doc = doc; |
939 | 5.77k | if (doc->children == NULL) { |
940 | 5.66k | doc->children = (xmlNodePtr) cur; |
941 | 5.66k | doc->last = (xmlNodePtr) cur; |
942 | 5.66k | } else { |
943 | 112 | if (doc->type == XML_HTML_DOCUMENT_NODE) { |
944 | 0 | xmlNodePtr prev; |
945 | |
|
946 | 0 | prev = doc->children; |
947 | 0 | prev->prev = (xmlNodePtr) cur; |
948 | 0 | cur->next = prev; |
949 | 0 | doc->children = (xmlNodePtr) cur; |
950 | 112 | } else { |
951 | 112 | xmlNodePtr next; |
952 | | |
953 | 112 | next = doc->children; |
954 | 487 | while ((next != NULL) && (next->type != XML_ELEMENT_NODE)) |
955 | 375 | next = next->next; |
956 | 112 | if (next == NULL) { |
957 | 112 | cur->prev = doc->last; |
958 | 112 | cur->prev->next = (xmlNodePtr) cur; |
959 | 112 | cur->next = NULL; |
960 | 112 | doc->last = (xmlNodePtr) cur; |
961 | 112 | } else { |
962 | 0 | cur->next = next; |
963 | 0 | cur->prev = next->prev; |
964 | 0 | if (cur->prev == NULL) |
965 | 0 | doc->children = (xmlNodePtr) cur; |
966 | 0 | else |
967 | 0 | cur->prev->next = (xmlNodePtr) cur; |
968 | 0 | next->prev = (xmlNodePtr) cur; |
969 | 0 | } |
970 | 112 | } |
971 | 112 | } |
972 | 5.77k | } |
973 | | |
974 | 5.77k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
975 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
976 | 5.77k | return(cur); |
977 | | |
978 | 0 | error: |
979 | 0 | xmlFreeDtd(cur); |
980 | 0 | return(NULL); |
981 | 5.77k | } |
982 | | |
983 | | /** |
984 | | * DICT_FREE: |
985 | | * @str: a string |
986 | | * |
987 | | * Free a string if it is not owned by the "dict" dictionary in the |
988 | | * current scope |
989 | | */ |
990 | | #define DICT_FREE(str) \ |
991 | 214k | if ((str) && ((!dict) || \ |
992 | 162k | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
993 | 70.1k | xmlFree((char *)(str)); |
994 | | |
995 | | /** |
996 | | * xmlFreeDtd: |
997 | | * @cur: the DTD structure to free up |
998 | | * |
999 | | * Free a DTD structure. |
1000 | | */ |
1001 | | void |
1002 | 6.24k | xmlFreeDtd(xmlDtdPtr cur) { |
1003 | 6.24k | xmlDictPtr dict = NULL; |
1004 | | |
1005 | 6.24k | if (cur == NULL) { |
1006 | 0 | return; |
1007 | 0 | } |
1008 | 6.24k | if (cur->doc != NULL) dict = cur->doc->dict; |
1009 | | |
1010 | 6.24k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
1011 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
1012 | | |
1013 | 6.24k | if (cur->children != NULL) { |
1014 | 5.10k | xmlNodePtr next, c = cur->children; |
1015 | | |
1016 | | /* |
1017 | | * Cleanup all nodes which are not part of the specific lists |
1018 | | * of notations, elements, attributes and entities. |
1019 | | */ |
1020 | 34.1k | while (c != NULL) { |
1021 | 29.0k | next = c->next; |
1022 | 29.0k | if ((c->type != XML_ELEMENT_DECL) && |
1023 | 29.0k | (c->type != XML_ATTRIBUTE_DECL) && |
1024 | 29.0k | (c->type != XML_ENTITY_DECL)) { |
1025 | 2.34k | xmlUnlinkNodeInternal(c); |
1026 | 2.34k | xmlFreeNode(c); |
1027 | 2.34k | } |
1028 | 29.0k | c = next; |
1029 | 29.0k | } |
1030 | 5.10k | } |
1031 | 6.24k | DICT_FREE(cur->name) |
1032 | 6.24k | DICT_FREE(cur->SystemID) |
1033 | 6.24k | DICT_FREE(cur->ExternalID) |
1034 | | /* TODO !!! */ |
1035 | 6.24k | if (cur->notations != NULL) |
1036 | 84 | xmlFreeNotationTable((xmlNotationTablePtr) cur->notations); |
1037 | | |
1038 | 6.24k | if (cur->elements != NULL) |
1039 | 2.09k | xmlFreeElementTable((xmlElementTablePtr) cur->elements); |
1040 | 6.24k | if (cur->attributes != NULL) |
1041 | 1.95k | xmlFreeAttributeTable((xmlAttributeTablePtr) cur->attributes); |
1042 | 6.24k | if (cur->entities != NULL) |
1043 | 2.05k | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->entities); |
1044 | 6.24k | if (cur->pentities != NULL) |
1045 | 1.55k | xmlFreeEntitiesTable((xmlEntitiesTablePtr) cur->pentities); |
1046 | | |
1047 | 6.24k | xmlFree(cur); |
1048 | 6.24k | } |
1049 | | |
1050 | | /** |
1051 | | * xmlNewDoc: |
1052 | | * @version: XML version string like "1.0" (optional) |
1053 | | * |
1054 | | * Creates a new XML document. If version is NULL, "1.0" is used. |
1055 | | * |
1056 | | * Returns a new document or NULL if a memory allocation failed. |
1057 | | */ |
1058 | | xmlDocPtr |
1059 | 15.5k | xmlNewDoc(const xmlChar *version) { |
1060 | 15.5k | xmlDocPtr cur; |
1061 | | |
1062 | 15.5k | if (version == NULL) |
1063 | 0 | version = (const xmlChar *) "1.0"; |
1064 | | |
1065 | | /* |
1066 | | * Allocate a new document and fill the fields. |
1067 | | */ |
1068 | 15.5k | cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc)); |
1069 | 15.5k | if (cur == NULL) |
1070 | 0 | return(NULL); |
1071 | 15.5k | memset(cur, 0, sizeof(xmlDoc)); |
1072 | 15.5k | cur->type = XML_DOCUMENT_NODE; |
1073 | | |
1074 | 15.5k | cur->version = xmlStrdup(version); |
1075 | 15.5k | if (cur->version == NULL) { |
1076 | 0 | xmlFree(cur); |
1077 | 0 | return(NULL); |
1078 | 0 | } |
1079 | 15.5k | cur->standalone = -1; |
1080 | 15.5k | cur->compression = -1; /* not initialized */ |
1081 | 15.5k | cur->doc = cur; |
1082 | 15.5k | cur->parseFlags = 0; |
1083 | 15.5k | cur->properties = XML_DOC_USERBUILT; |
1084 | | /* |
1085 | | * The in memory encoding is always UTF8 |
1086 | | * This field will never change and would |
1087 | | * be obsolete if not for binary compatibility. |
1088 | | */ |
1089 | 15.5k | cur->charset = XML_CHAR_ENCODING_UTF8; |
1090 | | |
1091 | 15.5k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1092 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1093 | 15.5k | return(cur); |
1094 | 15.5k | } |
1095 | | |
1096 | | /** |
1097 | | * xmlFreeDoc: |
1098 | | * @cur: pointer to the document |
1099 | | * |
1100 | | * Free a document including all children and associated DTDs. |
1101 | | */ |
1102 | | void |
1103 | 30.5k | xmlFreeDoc(xmlDocPtr cur) { |
1104 | 30.5k | xmlDtdPtr extSubset, intSubset; |
1105 | 30.5k | xmlDictPtr dict = NULL; |
1106 | | |
1107 | 30.5k | if (cur == NULL) { |
1108 | 15.0k | return; |
1109 | 15.0k | } |
1110 | | |
1111 | 15.5k | dict = cur->dict; |
1112 | | |
1113 | 15.5k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
1114 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
1115 | | |
1116 | | /* |
1117 | | * Do this before freeing the children list to avoid ID lookups |
1118 | | */ |
1119 | 15.5k | if (cur->ids != NULL) xmlFreeIDTable((xmlIDTablePtr) cur->ids); |
1120 | 15.5k | cur->ids = NULL; |
1121 | 15.5k | if (cur->refs != NULL) xmlFreeRefTable((xmlRefTablePtr) cur->refs); |
1122 | 15.5k | cur->refs = NULL; |
1123 | 15.5k | extSubset = cur->extSubset; |
1124 | 15.5k | intSubset = cur->intSubset; |
1125 | 15.5k | if (intSubset == extSubset) |
1126 | 9.78k | extSubset = NULL; |
1127 | 15.5k | if (extSubset != NULL) { |
1128 | 0 | xmlUnlinkNodeInternal((xmlNodePtr) cur->extSubset); |
1129 | 0 | cur->extSubset = NULL; |
1130 | 0 | xmlFreeDtd(extSubset); |
1131 | 0 | } |
1132 | 15.5k | if (intSubset != NULL) { |
1133 | 6.24k | xmlUnlinkNodeInternal((xmlNodePtr) cur->intSubset); |
1134 | 6.24k | cur->intSubset = NULL; |
1135 | 6.24k | xmlFreeDtd(intSubset); |
1136 | 6.24k | } |
1137 | | |
1138 | 15.5k | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
1139 | 15.5k | if (cur->oldNs != NULL) xmlFreeNsList(cur->oldNs); |
1140 | | |
1141 | 15.5k | DICT_FREE(cur->version) |
1142 | 15.5k | DICT_FREE(cur->name) |
1143 | 15.5k | DICT_FREE(cur->encoding) |
1144 | 15.5k | DICT_FREE(cur->URL) |
1145 | 15.5k | xmlFree(cur); |
1146 | 15.5k | if (dict) xmlDictFree(dict); |
1147 | 15.5k | } |
1148 | | |
1149 | | /** |
1150 | | * xmlNodeParseContentInternal: |
1151 | | * @doc: a document (optional) |
1152 | | * @parent: an element or attribute (optional) |
1153 | | * @value: an attribute value |
1154 | | * @len: maximum length of the attribute value |
1155 | | * @listPtr: pointer to the resulting node list (optional) |
1156 | | * |
1157 | | * See xmlNodeParseContent. |
1158 | | * |
1159 | | * Returns 0 on success, -1 if a memory allocation failed. |
1160 | | */ |
1161 | | static int |
1162 | | xmlNodeParseContentInternal(const xmlDoc *doc, xmlNodePtr parent, |
1163 | | const xmlChar *value, int len, |
1164 | 5.20k | xmlNodePtr *listPtr) { |
1165 | 5.20k | xmlNodePtr head = NULL, last = NULL; |
1166 | 5.20k | xmlNodePtr node; |
1167 | 5.20k | xmlChar *val = NULL; |
1168 | 5.20k | const xmlChar *cur; |
1169 | 5.20k | const xmlChar *q; |
1170 | 5.20k | xmlEntityPtr ent; |
1171 | 5.20k | xmlBufPtr buf; |
1172 | 5.20k | int remaining; |
1173 | | |
1174 | 5.20k | if (listPtr != NULL) |
1175 | 0 | *listPtr = NULL; |
1176 | | |
1177 | 5.20k | if (len < 0) |
1178 | 604 | remaining = INT_MAX; |
1179 | 4.60k | else |
1180 | 4.60k | remaining = len; |
1181 | | |
1182 | 5.20k | if ((value == NULL) || (value[0] == 0)) |
1183 | 28 | goto done; |
1184 | | |
1185 | 5.17k | cur = value; |
1186 | | |
1187 | 5.17k | buf = xmlBufCreate(50); |
1188 | 5.17k | if (buf == NULL) |
1189 | 0 | return(-1); |
1190 | | |
1191 | 5.17k | q = cur; |
1192 | 28.8M | while ((remaining > 0) && (*cur != 0)) { |
1193 | 28.8M | if (cur[0] == '&') { |
1194 | 7.56k | int charval = 0; |
1195 | | |
1196 | | /* |
1197 | | * Save the current text. |
1198 | | */ |
1199 | 7.56k | if (cur != q) { |
1200 | 3.72k | if (xmlBufAdd(buf, q, cur - q)) |
1201 | 0 | goto out; |
1202 | 3.72k | q = cur; |
1203 | 3.72k | } |
1204 | | |
1205 | 7.56k | if ((remaining > 2) && (cur[1] == '#') && (cur[2] == 'x')) { |
1206 | 666 | int tmp = 0; |
1207 | | |
1208 | 666 | cur += 3; |
1209 | 666 | remaining -= 3; |
1210 | 2.59k | while ((remaining > 0) && ((tmp = *cur) != ';')) { |
1211 | 1.93k | if ((tmp >= '0') && (tmp <= '9')) |
1212 | 589 | charval = charval * 16 + (tmp - '0'); |
1213 | 1.34k | else if ((tmp >= 'a') && (tmp <= 'f')) |
1214 | 184 | charval = charval * 16 + (tmp - 'a') + 10; |
1215 | 1.16k | else if ((tmp >= 'A') && (tmp <= 'F')) |
1216 | 1.16k | charval = charval * 16 + (tmp - 'A') + 10; |
1217 | 0 | else { |
1218 | 0 | charval = 0; |
1219 | 0 | break; |
1220 | 0 | } |
1221 | 1.93k | if (charval > 0x110000) |
1222 | 0 | charval = 0x110000; |
1223 | 1.93k | cur++; |
1224 | 1.93k | remaining--; |
1225 | 1.93k | } |
1226 | 666 | if (tmp == ';') { |
1227 | 666 | cur++; |
1228 | 666 | remaining--; |
1229 | 666 | } |
1230 | 666 | q = cur; |
1231 | 6.90k | } else if ((remaining > 1) && (cur[1] == '#')) { |
1232 | 790 | int tmp = 0; |
1233 | | |
1234 | 790 | cur += 2; |
1235 | 790 | remaining -= 2; |
1236 | 2.53k | while ((remaining > 0) && ((tmp = *cur) != ';')) { |
1237 | 1.74k | if ((tmp >= '0') && (tmp <= '9')) |
1238 | 1.74k | charval = charval * 10 + (tmp - '0'); |
1239 | 0 | else { |
1240 | 0 | charval = 0; |
1241 | 0 | break; |
1242 | 0 | } |
1243 | 1.74k | if (charval > 0x110000) |
1244 | 0 | charval = 0x110000; |
1245 | 1.74k | cur++; |
1246 | 1.74k | remaining--; |
1247 | 1.74k | } |
1248 | 790 | if (tmp == ';') { |
1249 | 790 | cur++; |
1250 | 790 | remaining--; |
1251 | 790 | } |
1252 | 790 | q = cur; |
1253 | 6.11k | } else { |
1254 | | /* |
1255 | | * Read the entity string |
1256 | | */ |
1257 | 6.11k | cur++; |
1258 | 6.11k | remaining--; |
1259 | 6.11k | q = cur; |
1260 | 15.6k | while ((remaining > 0) && (*cur != 0) && (*cur != ';')) { |
1261 | 9.49k | cur++; |
1262 | 9.49k | remaining--; |
1263 | 9.49k | } |
1264 | 6.11k | if ((remaining <= 0) || (*cur == 0)) |
1265 | 0 | break; |
1266 | 6.11k | if (cur != q) { |
1267 | 6.11k | val = xmlStrndup(q, cur - q); |
1268 | 6.11k | if (val == NULL) |
1269 | 0 | goto out; |
1270 | 6.11k | ent = xmlGetDocEntity(doc, val); |
1271 | 6.11k | if ((ent != NULL) && |
1272 | 6.11k | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
1273 | | /* |
1274 | | * Predefined entities don't generate nodes |
1275 | | */ |
1276 | 1.57k | if (xmlBufCat(buf, ent->content)) |
1277 | 0 | goto out; |
1278 | 4.53k | } else { |
1279 | | /* |
1280 | | * Flush buffer so far |
1281 | | */ |
1282 | 4.53k | if (!xmlBufIsEmpty(buf)) { |
1283 | 1.38k | node = xmlNewDocText(doc, NULL); |
1284 | 1.38k | if (node == NULL) |
1285 | 0 | goto out; |
1286 | 1.38k | node->content = xmlBufDetach(buf); |
1287 | 1.38k | node->parent = parent; |
1288 | | |
1289 | 1.38k | if (last == NULL) { |
1290 | 356 | head = node; |
1291 | 1.03k | } else { |
1292 | 1.03k | last->next = node; |
1293 | 1.03k | node->prev = last; |
1294 | 1.03k | } |
1295 | 1.38k | last = node; |
1296 | 1.38k | } |
1297 | | |
1298 | 4.53k | if ((ent != NULL) && |
1299 | 4.53k | ((ent->flags & XML_ENT_PARSED) == 0) && |
1300 | 4.53k | ((ent->flags & XML_ENT_EXPANDING) == 0) && |
1301 | 4.53k | (ent->content != NULL)) { |
1302 | 604 | int res; |
1303 | | |
1304 | 604 | ent->flags |= XML_ENT_EXPANDING; |
1305 | 604 | res = xmlNodeParseContentInternal(doc, |
1306 | 604 | (xmlNodePtr) ent, ent->content, -1, NULL); |
1307 | 604 | ent->flags &= ~XML_ENT_EXPANDING; |
1308 | 604 | if (res < 0) |
1309 | 0 | goto out; |
1310 | 604 | ent->flags |= XML_ENT_PARSED; |
1311 | 604 | } |
1312 | | |
1313 | | /* |
1314 | | * Create a new REFERENCE_REF node |
1315 | | */ |
1316 | 4.53k | node = xmlNewEntityRef((xmlDocPtr) doc, val); |
1317 | 4.53k | val = NULL; |
1318 | 4.53k | if (node == NULL) |
1319 | 0 | goto out; |
1320 | 4.53k | node->parent = parent; |
1321 | 4.53k | node->last = (xmlNodePtr) ent; |
1322 | 4.53k | if (ent != NULL) { |
1323 | 4.34k | node->children = (xmlNodePtr) ent; |
1324 | 4.34k | node->content = ent->content; |
1325 | 4.34k | } |
1326 | | |
1327 | 4.53k | if (last == NULL) { |
1328 | 834 | head = node; |
1329 | 3.70k | } else { |
1330 | 3.70k | last->next = node; |
1331 | 3.70k | node->prev = last; |
1332 | 3.70k | } |
1333 | 4.53k | last = node; |
1334 | 4.53k | } |
1335 | 6.11k | xmlFree(val); |
1336 | 6.11k | val = NULL; |
1337 | 6.11k | } |
1338 | 6.11k | cur++; |
1339 | 6.11k | remaining--; |
1340 | 6.11k | q = cur; |
1341 | 6.11k | } |
1342 | 7.56k | if (charval != 0) { |
1343 | 1.45k | xmlChar buffer[10]; |
1344 | 1.45k | int l; |
1345 | | |
1346 | 1.45k | if (charval >= 0x110000) |
1347 | 0 | charval = 0xFFFD; /* replacement character */ |
1348 | | |
1349 | 1.45k | l = xmlCopyCharMultiByte(buffer, charval); |
1350 | 1.45k | buffer[l] = 0; |
1351 | | |
1352 | 1.45k | if (xmlBufCat(buf, buffer)) |
1353 | 0 | goto out; |
1354 | 1.45k | } |
1355 | 28.8M | } else { |
1356 | 28.8M | cur++; |
1357 | 28.8M | remaining--; |
1358 | 28.8M | } |
1359 | 28.8M | } |
1360 | | |
1361 | 5.17k | if (cur != q) { |
1362 | | /* |
1363 | | * Handle the last piece of text. |
1364 | | */ |
1365 | 4.26k | if (xmlBufAdd(buf, q, cur - q)) |
1366 | 0 | goto out; |
1367 | 4.26k | } |
1368 | | |
1369 | 5.17k | if (!xmlBufIsEmpty(buf)) { |
1370 | 4.51k | node = xmlNewDocText(doc, NULL); |
1371 | 4.51k | if (node == NULL) |
1372 | 0 | goto out; |
1373 | 4.51k | node->parent = parent; |
1374 | 4.51k | node->content = xmlBufDetach(buf); |
1375 | | |
1376 | 4.51k | if (last == NULL) { |
1377 | 3.98k | head = node; |
1378 | 3.98k | } else { |
1379 | 525 | last->next = node; |
1380 | 525 | node->prev = last; |
1381 | 525 | } |
1382 | 4.51k | last = node; |
1383 | 4.51k | } else if (head == NULL) { |
1384 | 0 | head = xmlNewDocText(doc, BAD_CAST ""); |
1385 | 0 | if (head == NULL) |
1386 | 0 | goto out; |
1387 | 0 | head->parent = parent; |
1388 | 0 | last = head; |
1389 | 0 | } |
1390 | | |
1391 | 5.17k | xmlBufFree(buf); |
1392 | | |
1393 | 5.20k | done: |
1394 | 5.20k | if (parent != NULL) { |
1395 | 5.20k | if (parent->children != NULL) |
1396 | 0 | xmlFreeNodeList(parent->children); |
1397 | 5.20k | parent->children = head; |
1398 | 5.20k | parent->last = last; |
1399 | 5.20k | } |
1400 | | |
1401 | 5.20k | if (listPtr != NULL) |
1402 | 0 | *listPtr = head; |
1403 | | |
1404 | 5.20k | return(0); |
1405 | | |
1406 | 0 | out: |
1407 | 0 | xmlBufFree(buf); |
1408 | 0 | if (val != NULL) |
1409 | 0 | xmlFree(val); |
1410 | 0 | if (head != NULL) |
1411 | 0 | xmlFreeNodeList(head); |
1412 | 0 | return(-1); |
1413 | 5.17k | } |
1414 | | |
1415 | | /** |
1416 | | * xmlNodeParseContent: |
1417 | | * @node: an element or attribute |
1418 | | * @content: text content with XML references |
1419 | | * @len: maximum length of content |
1420 | | * |
1421 | | * Parse content and replace the node's children with the resulting |
1422 | | * node list. |
1423 | | * |
1424 | | * @content is expected to be a valid XML attribute value possibly |
1425 | | * containing character and entity references. Syntax errors |
1426 | | * and references to undeclared entities are ignored silently. |
1427 | | * Only references are handled, nested elements, comments or PIs are |
1428 | | * not. |
1429 | | * |
1430 | | * Returns 0 on success, -1 if a memory allocation failed. |
1431 | | */ |
1432 | | int |
1433 | 4.60k | xmlNodeParseContent(xmlNodePtr node, const xmlChar *content, int len) { |
1434 | 4.60k | return(xmlNodeParseContentInternal(node->doc, node, content, len, NULL)); |
1435 | 4.60k | } |
1436 | | |
1437 | | /** |
1438 | | * xmlStringLenGetNodeList: |
1439 | | * @doc: a document (optional) |
1440 | | * @value: an attribute value |
1441 | | * @len: maximum length of the attribute value |
1442 | | * |
1443 | | * DEPRECATED: Use xmlNodeSetContentLen. |
1444 | | * |
1445 | | * See xmlStringGetNodeList. |
1446 | | * |
1447 | | * Returns a pointer to the first child or NULL if the value if empty |
1448 | | * or a memory allocation failed. |
1449 | | */ |
1450 | | xmlNodePtr |
1451 | 0 | xmlStringLenGetNodeList(const xmlDoc *doc, const xmlChar *value, int len) { |
1452 | 0 | xmlNodePtr ret; |
1453 | |
|
1454 | 0 | xmlNodeParseContentInternal(doc, NULL, value, len, &ret); |
1455 | 0 | return(ret); |
1456 | 0 | } |
1457 | | |
1458 | | /** |
1459 | | * xmlStringGetNodeList: |
1460 | | * @doc: a document (optional) |
1461 | | * @value: an attribute value |
1462 | | * |
1463 | | * DEPRECATED: Use xmlNodeSetContent. |
1464 | | * |
1465 | | * Parse an attribute value and build a node list containing only |
1466 | | * text and entity reference nodes. The resulting nodes will be |
1467 | | * associated with the document if provided. The document is also |
1468 | | * used to look up entities. |
1469 | | * |
1470 | | * The input is not validated. Syntax errors or references to |
1471 | | * undeclared entities will be ignored silently with unspecified |
1472 | | * results. |
1473 | | * |
1474 | | * Returns a pointer to the first child or NULL if the value if empty |
1475 | | * or a memory allocation failed. |
1476 | | */ |
1477 | | xmlNodePtr |
1478 | 0 | xmlStringGetNodeList(const xmlDoc *doc, const xmlChar *value) { |
1479 | 0 | xmlNodePtr ret; |
1480 | |
|
1481 | 0 | xmlNodeParseContentInternal(doc, NULL, value, -1, &ret); |
1482 | 0 | return(ret); |
1483 | 0 | } |
1484 | | |
1485 | | /** |
1486 | | * xmlNodeListGetStringInternal: |
1487 | | * @doc: a document (optional) |
1488 | | * @node: a node list |
1489 | | * @escMode: escape mode (0 = no, 1 = elem, 2 = attr, 3 = raw) |
1490 | | * |
1491 | | * Returns a pointer to the string. |
1492 | | */ |
1493 | | static xmlChar * |
1494 | 201 | xmlNodeListGetStringInternal(xmlDocPtr doc, const xmlNode *node, int escMode) { |
1495 | 201 | xmlBufPtr buf; |
1496 | 201 | xmlChar *ret; |
1497 | | |
1498 | 201 | if (node == NULL) |
1499 | 0 | return(xmlStrdup(BAD_CAST "")); |
1500 | | |
1501 | 201 | if ((escMode == 0) && |
1502 | 201 | ((node->type == XML_TEXT_NODE) || |
1503 | 201 | (node->type == XML_CDATA_SECTION_NODE)) && |
1504 | 201 | (node->next == NULL)) { |
1505 | 199 | if (node->content == NULL) |
1506 | 0 | return(xmlStrdup(BAD_CAST "")); |
1507 | 199 | return(xmlStrdup(node->content)); |
1508 | 199 | } |
1509 | | |
1510 | 2 | buf = xmlBufCreate(50); |
1511 | 2 | if (buf == NULL) |
1512 | 0 | return(NULL); |
1513 | | |
1514 | 4 | while (node != NULL) { |
1515 | 2 | if ((node->type == XML_TEXT_NODE) || |
1516 | 2 | (node->type == XML_CDATA_SECTION_NODE)) { |
1517 | 0 | if (node->content != NULL) { |
1518 | 0 | if (escMode == 0) { |
1519 | 0 | xmlBufCat(buf, node->content); |
1520 | 0 | } else { |
1521 | 0 | xmlChar *encoded; |
1522 | |
|
1523 | 0 | if (escMode == 1) |
1524 | 0 | encoded = xmlEncodeEntitiesInternal(doc, node->content, |
1525 | 0 | 0); |
1526 | 0 | else if (escMode == 2) |
1527 | 0 | encoded = xmlEncodeEntitiesInternal(doc, node->content, |
1528 | 0 | XML_ESCAPE_ATTR); |
1529 | 0 | else |
1530 | 0 | encoded = xmlEncodeSpecialChars(doc, node->content); |
1531 | 0 | if (encoded == NULL) |
1532 | 0 | goto error; |
1533 | 0 | xmlBufCat(buf, encoded); |
1534 | 0 | xmlFree(encoded); |
1535 | 0 | } |
1536 | 0 | } |
1537 | 2 | } else if (node->type == XML_ENTITY_REF_NODE) { |
1538 | 0 | if (escMode == 0) { |
1539 | 0 | xmlBufGetNodeContent(buf, node); |
1540 | 0 | } else { |
1541 | 0 | xmlBufAdd(buf, BAD_CAST "&", 1); |
1542 | 0 | xmlBufCat(buf, node->name); |
1543 | 0 | xmlBufAdd(buf, BAD_CAST ";", 1); |
1544 | 0 | } |
1545 | 0 | } |
1546 | | |
1547 | 2 | node = node->next; |
1548 | 2 | } |
1549 | | |
1550 | 2 | ret = xmlBufDetach(buf); |
1551 | 2 | xmlBufFree(buf); |
1552 | 2 | return(ret); |
1553 | | |
1554 | 0 | error: |
1555 | 0 | xmlBufFree(buf); |
1556 | 0 | return(NULL); |
1557 | 2 | } |
1558 | | |
1559 | | /** |
1560 | | * xmlNodeListGetString: |
1561 | | * @doc: a document (optional) |
1562 | | * @list: a node list of attribute children |
1563 | | * @inLine: whether entity references are substituted |
1564 | | * |
1565 | | * Serializes attribute children (text and entity reference nodes) |
1566 | | * into a string. |
1567 | | * |
1568 | | * If @inLine is true, entity references will be substituted. |
1569 | | * Otherwise, entity references will be kept and special characters |
1570 | | * like '&' as well as non-ASCII chars will be escaped. See |
1571 | | * xmlNodeListGetRawString for an alternative option. |
1572 | | * |
1573 | | * Returns a string or NULL if a memory allocation failed. |
1574 | | */ |
1575 | | xmlChar * |
1576 | | xmlNodeListGetString(xmlDocPtr doc, const xmlNode *list, int inLine) |
1577 | 201 | { |
1578 | 201 | int escMode; |
1579 | | |
1580 | | /* backward compatibility */ |
1581 | 201 | if (list == NULL) |
1582 | 0 | return(NULL); |
1583 | | |
1584 | 201 | if (inLine) { |
1585 | 201 | escMode = 0; |
1586 | 201 | } else { |
1587 | 0 | if ((list->parent != NULL) && |
1588 | 0 | (list->parent->type == XML_ATTRIBUTE_NODE)) |
1589 | 0 | escMode = 2; |
1590 | 0 | else |
1591 | 0 | escMode = 1; |
1592 | 0 | } |
1593 | | |
1594 | 201 | return(xmlNodeListGetStringInternal(doc, list, escMode)); |
1595 | 201 | } |
1596 | | |
1597 | | /** |
1598 | | * xmlNodeListGetRawString: |
1599 | | * @doc: a document (optional) |
1600 | | * @list: a node list of attribute children |
1601 | | * @inLine: whether entity references are substituted |
1602 | | * |
1603 | | * Serializes attribute children (text and entity reference nodes) |
1604 | | * into a string. |
1605 | | * |
1606 | | * If @inLine is true, entity references will be substituted. |
1607 | | * Otherwise, entity references will be kept and special characters |
1608 | | * like '&' will be escaped. |
1609 | | * |
1610 | | * Returns a string or NULL if a memory allocation failed. |
1611 | | */ |
1612 | | xmlChar * |
1613 | | xmlNodeListGetRawString(const xmlDoc *doc, const xmlNode *list, int inLine) |
1614 | 0 | { |
1615 | 0 | int escMode = inLine ? 0 : 3; |
1616 | | |
1617 | | /* backward compatibility */ |
1618 | 0 | if (list == NULL) |
1619 | 0 | return(NULL); |
1620 | | |
1621 | 0 | return(xmlNodeListGetStringInternal((xmlDocPtr) doc, list, escMode)); |
1622 | 0 | } |
1623 | | |
1624 | | static xmlAttrPtr |
1625 | | xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, |
1626 | | const xmlChar * name, const xmlChar * value, |
1627 | | int eatname) |
1628 | 0 | { |
1629 | 0 | xmlAttrPtr cur; |
1630 | 0 | xmlDocPtr doc = NULL; |
1631 | |
|
1632 | 0 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) { |
1633 | 0 | if ((eatname == 1) && |
1634 | 0 | ((node->doc == NULL) || (node->doc->dict == NULL) || |
1635 | 0 | (!(xmlDictOwns(node->doc->dict, name))))) |
1636 | 0 | xmlFree((xmlChar *) name); |
1637 | 0 | return (NULL); |
1638 | 0 | } |
1639 | | |
1640 | | /* |
1641 | | * Allocate a new property and fill the fields. |
1642 | | */ |
1643 | 0 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
1644 | 0 | if (cur == NULL) { |
1645 | 0 | if ((eatname == 1) && |
1646 | 0 | ((node == NULL) || (node->doc == NULL) || |
1647 | 0 | (node->doc->dict == NULL) || |
1648 | 0 | (!(xmlDictOwns(node->doc->dict, name))))) |
1649 | 0 | xmlFree((xmlChar *) name); |
1650 | 0 | return (NULL); |
1651 | 0 | } |
1652 | 0 | memset(cur, 0, sizeof(xmlAttr)); |
1653 | 0 | cur->type = XML_ATTRIBUTE_NODE; |
1654 | |
|
1655 | 0 | cur->parent = node; |
1656 | 0 | if (node != NULL) { |
1657 | 0 | doc = node->doc; |
1658 | 0 | cur->doc = doc; |
1659 | 0 | } |
1660 | 0 | cur->ns = ns; |
1661 | |
|
1662 | 0 | if (eatname == 0) { |
1663 | 0 | if ((doc != NULL) && (doc->dict != NULL)) |
1664 | 0 | cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1); |
1665 | 0 | else |
1666 | 0 | cur->name = xmlStrdup(name); |
1667 | 0 | if (cur->name == NULL) |
1668 | 0 | goto error; |
1669 | 0 | } else |
1670 | 0 | cur->name = name; |
1671 | | |
1672 | 0 | if (value != NULL) { |
1673 | 0 | xmlNodePtr tmp; |
1674 | |
|
1675 | 0 | cur->children = xmlNewDocText(doc, value); |
1676 | 0 | if (cur->children == NULL) |
1677 | 0 | goto error; |
1678 | 0 | cur->last = NULL; |
1679 | 0 | tmp = cur->children; |
1680 | 0 | while (tmp != NULL) { |
1681 | 0 | tmp->parent = (xmlNodePtr) cur; |
1682 | 0 | if (tmp->next == NULL) |
1683 | 0 | cur->last = tmp; |
1684 | 0 | tmp = tmp->next; |
1685 | 0 | } |
1686 | |
|
1687 | 0 | if (doc != NULL) { |
1688 | 0 | int res = xmlIsID(doc, node, cur); |
1689 | |
|
1690 | 0 | if (res < 0) |
1691 | 0 | goto error; |
1692 | 0 | if ((res == 1) && (xmlAddIDSafe(cur, value) < 0)) |
1693 | 0 | goto error; |
1694 | 0 | } |
1695 | 0 | } |
1696 | | |
1697 | | /* |
1698 | | * Add it at the end to preserve parsing order ... |
1699 | | */ |
1700 | 0 | if (node != NULL) { |
1701 | 0 | if (node->properties == NULL) { |
1702 | 0 | node->properties = cur; |
1703 | 0 | } else { |
1704 | 0 | xmlAttrPtr prev = node->properties; |
1705 | |
|
1706 | 0 | while (prev->next != NULL) |
1707 | 0 | prev = prev->next; |
1708 | 0 | prev->next = cur; |
1709 | 0 | cur->prev = prev; |
1710 | 0 | } |
1711 | 0 | } |
1712 | |
|
1713 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1714 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr) cur); |
1715 | 0 | return (cur); |
1716 | | |
1717 | 0 | error: |
1718 | 0 | xmlFreeProp(cur); |
1719 | 0 | return(NULL); |
1720 | 0 | } |
1721 | | |
1722 | | /** |
1723 | | * xmlNewProp: |
1724 | | * @node: the parent node (optional) |
1725 | | * @name: the name of the attribute |
1726 | | * @value: the value of the attribute (optional) |
1727 | | * |
1728 | | * Create an attribute node. |
1729 | | * |
1730 | | * If provided, @value should be a raw, unescaped string. |
1731 | | * |
1732 | | * If @node is provided, the created attribute will be appended without |
1733 | | * checking for duplicate names. It is an error if @node is not an |
1734 | | * element. |
1735 | | * |
1736 | | * Returns a pointer to the attribute or NULL if arguments are invalid |
1737 | | * or a memory allocation failed. |
1738 | | */ |
1739 | | xmlAttrPtr |
1740 | 0 | xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
1741 | |
|
1742 | 0 | if (name == NULL) { |
1743 | 0 | return(NULL); |
1744 | 0 | } |
1745 | | |
1746 | 0 | return xmlNewPropInternal(node, NULL, name, value, 0); |
1747 | 0 | } |
1748 | | |
1749 | | /** |
1750 | | * xmlNewNsProp: |
1751 | | * @node: the parent node (optional) |
1752 | | * @ns: the namespace (optional) |
1753 | | * @name: the local name of the attribute |
1754 | | * @value: the value of the attribute (optional) |
1755 | | * |
1756 | | * Create an attribute object. |
1757 | | * |
1758 | | * If provided, @value should be a raw, unescaped string. |
1759 | | * |
1760 | | * If @node is provided, the created attribute will be appended without |
1761 | | * checking for duplicate names. It is an error if @node is not an |
1762 | | * element. |
1763 | | * |
1764 | | * Returns a pointer to the attribute or NULL if arguments are invalid |
1765 | | * or a memory allocation failed. |
1766 | | */ |
1767 | | xmlAttrPtr |
1768 | | xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
1769 | 0 | const xmlChar *value) { |
1770 | |
|
1771 | 0 | if (name == NULL) { |
1772 | 0 | return(NULL); |
1773 | 0 | } |
1774 | | |
1775 | 0 | return xmlNewPropInternal(node, ns, name, value, 0); |
1776 | 0 | } |
1777 | | |
1778 | | /** |
1779 | | * xmlNewNsPropEatName: |
1780 | | * @node: the parent node (optional) |
1781 | | * @ns: the namespace (optional) |
1782 | | * @name: the local name of the attribute |
1783 | | * @value: the value of the attribute (optional) |
1784 | | * |
1785 | | * Like xmlNewNsProp, but the @name string will be used directly |
1786 | | * without making a copy. Takes ownership of @name which will also |
1787 | | * be freed on error. |
1788 | | * |
1789 | | * Returns a pointer to the attribute or NULL if arguments are invalid |
1790 | | * or a memory allocation failed. |
1791 | | */ |
1792 | | xmlAttrPtr |
1793 | | xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name, |
1794 | 0 | const xmlChar *value) { |
1795 | |
|
1796 | 0 | if (name == NULL) { |
1797 | 0 | return(NULL); |
1798 | 0 | } |
1799 | | |
1800 | 0 | return xmlNewPropInternal(node, ns, name, value, 1); |
1801 | 0 | } |
1802 | | |
1803 | | /** |
1804 | | * xmlNewDocProp: |
1805 | | * @doc: the target document (optional) |
1806 | | * @name: the name of the attribute |
1807 | | * @value: attribute value with XML references (optional) |
1808 | | * |
1809 | | * Create an attribute object. |
1810 | | * |
1811 | | * If provided, @value is expected to be a valid XML attribute value |
1812 | | * possibly containing character and entity references. Syntax errors |
1813 | | * and references to undeclared entities are ignored silently. |
1814 | | * If you want to pass a raw string, see xmlNewProp. |
1815 | | * |
1816 | | * Returns a pointer to the attribute or NULL if arguments are invalid |
1817 | | * or a memory allocation failed. |
1818 | | */ |
1819 | | xmlAttrPtr |
1820 | 0 | xmlNewDocProp(xmlDocPtr doc, const xmlChar *name, const xmlChar *value) { |
1821 | 0 | xmlAttrPtr cur; |
1822 | |
|
1823 | 0 | if (name == NULL) { |
1824 | 0 | return(NULL); |
1825 | 0 | } |
1826 | | |
1827 | | /* |
1828 | | * Allocate a new property and fill the fields. |
1829 | | */ |
1830 | 0 | cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr)); |
1831 | 0 | if (cur == NULL) |
1832 | 0 | return(NULL); |
1833 | 0 | memset(cur, 0, sizeof(xmlAttr)); |
1834 | 0 | cur->type = XML_ATTRIBUTE_NODE; |
1835 | |
|
1836 | 0 | if ((doc != NULL) && (doc->dict != NULL)) |
1837 | 0 | cur->name = xmlDictLookup(doc->dict, name, -1); |
1838 | 0 | else |
1839 | 0 | cur->name = xmlStrdup(name); |
1840 | 0 | if (cur->name == NULL) |
1841 | 0 | goto error; |
1842 | 0 | cur->doc = doc; |
1843 | 0 | if (value != NULL) { |
1844 | 0 | if (xmlNodeParseContent((xmlNodePtr) cur, value, -1) < 0) |
1845 | 0 | goto error; |
1846 | 0 | } |
1847 | | |
1848 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1849 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1850 | 0 | return(cur); |
1851 | | |
1852 | 0 | error: |
1853 | 0 | xmlFreeProp(cur); |
1854 | 0 | return(NULL); |
1855 | 0 | } |
1856 | | |
1857 | | /** |
1858 | | * xmlFreePropList: |
1859 | | * @cur: the first attribute in the list |
1860 | | * |
1861 | | * Free an attribute list including all children. |
1862 | | */ |
1863 | | void |
1864 | 16.8k | xmlFreePropList(xmlAttrPtr cur) { |
1865 | 16.8k | xmlAttrPtr next; |
1866 | 16.8k | if (cur == NULL) return; |
1867 | 40.4k | while (cur != NULL) { |
1868 | 23.6k | next = cur->next; |
1869 | 23.6k | xmlFreeProp(cur); |
1870 | 23.6k | cur = next; |
1871 | 23.6k | } |
1872 | 16.8k | } |
1873 | | |
1874 | | /** |
1875 | | * xmlFreeProp: |
1876 | | * @cur: an attribute |
1877 | | * |
1878 | | * Free an attribute including all children. |
1879 | | */ |
1880 | | void |
1881 | 23.6k | xmlFreeProp(xmlAttrPtr cur) { |
1882 | 23.6k | xmlDictPtr dict = NULL; |
1883 | 23.6k | if (cur == NULL) return; |
1884 | | |
1885 | 23.6k | if (cur->doc != NULL) dict = cur->doc->dict; |
1886 | | |
1887 | 23.6k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
1888 | 0 | xmlDeregisterNodeDefaultValue((xmlNodePtr)cur); |
1889 | | |
1890 | | /* Check for ID removal -> leading to invalid references ! */ |
1891 | 23.6k | if ((cur->doc != NULL) && (cur->atype == XML_ATTRIBUTE_ID)) { |
1892 | 0 | xmlRemoveID(cur->doc, cur); |
1893 | 0 | } |
1894 | 23.6k | if (cur->children != NULL) xmlFreeNodeList(cur->children); |
1895 | 23.6k | DICT_FREE(cur->name) |
1896 | 23.6k | xmlFree(cur); |
1897 | 23.6k | } |
1898 | | |
1899 | | /** |
1900 | | * xmlRemoveProp: |
1901 | | * @cur: an attribute |
1902 | | * |
1903 | | * Unlink and free an attribute including all children. |
1904 | | * |
1905 | | * Note this doesn't work for namespace declarations. |
1906 | | * |
1907 | | * The attribute must have a non-NULL parent pointer. |
1908 | | * |
1909 | | * Returns 0 on success or -1 if the attribute was not found or |
1910 | | * arguments are invalid. |
1911 | | */ |
1912 | | int |
1913 | 0 | xmlRemoveProp(xmlAttrPtr cur) { |
1914 | 0 | xmlAttrPtr tmp; |
1915 | 0 | if (cur == NULL) { |
1916 | 0 | return(-1); |
1917 | 0 | } |
1918 | 0 | if (cur->parent == NULL) { |
1919 | 0 | return(-1); |
1920 | 0 | } |
1921 | 0 | tmp = cur->parent->properties; |
1922 | 0 | if (tmp == cur) { |
1923 | 0 | cur->parent->properties = cur->next; |
1924 | 0 | if (cur->next != NULL) |
1925 | 0 | cur->next->prev = NULL; |
1926 | 0 | xmlFreeProp(cur); |
1927 | 0 | return(0); |
1928 | 0 | } |
1929 | 0 | while (tmp != NULL) { |
1930 | 0 | if (tmp->next == cur) { |
1931 | 0 | tmp->next = cur->next; |
1932 | 0 | if (tmp->next != NULL) |
1933 | 0 | tmp->next->prev = tmp; |
1934 | 0 | xmlFreeProp(cur); |
1935 | 0 | return(0); |
1936 | 0 | } |
1937 | 0 | tmp = tmp->next; |
1938 | 0 | } |
1939 | 0 | return(-1); |
1940 | 0 | } |
1941 | | |
1942 | | /** |
1943 | | * xmlNewDocPI: |
1944 | | * @doc: the target document (optional) |
1945 | | * @name: the processing instruction target |
1946 | | * @content: the PI content (optional) |
1947 | | * |
1948 | | * Create a processing instruction object. |
1949 | | * |
1950 | | * Returns a pointer to the new node object or NULL if arguments are |
1951 | | * invalid or a memory allocation failed. |
1952 | | */ |
1953 | | xmlNodePtr |
1954 | 5.60k | xmlNewDocPI(xmlDocPtr doc, const xmlChar *name, const xmlChar *content) { |
1955 | 5.60k | xmlNodePtr cur; |
1956 | | |
1957 | 5.60k | if (name == NULL) { |
1958 | 0 | return(NULL); |
1959 | 0 | } |
1960 | | |
1961 | | /* |
1962 | | * Allocate a new node and fill the fields. |
1963 | | */ |
1964 | 5.60k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
1965 | 5.60k | if (cur == NULL) |
1966 | 0 | return(NULL); |
1967 | 5.60k | memset(cur, 0, sizeof(xmlNode)); |
1968 | 5.60k | cur->type = XML_PI_NODE; |
1969 | 5.60k | cur->doc = doc; |
1970 | | |
1971 | 5.60k | if ((doc != NULL) && (doc->dict != NULL)) |
1972 | 5.60k | cur->name = xmlDictLookup(doc->dict, name, -1); |
1973 | 0 | else |
1974 | 0 | cur->name = xmlStrdup(name); |
1975 | 5.60k | if (cur->name == NULL) |
1976 | 0 | goto error; |
1977 | 5.60k | if (content != NULL) { |
1978 | 2.28k | cur->content = xmlStrdup(content); |
1979 | 2.28k | if (cur->content == NULL) |
1980 | 0 | goto error; |
1981 | 2.28k | } |
1982 | | |
1983 | 5.60k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
1984 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
1985 | 5.60k | return(cur); |
1986 | | |
1987 | 0 | error: |
1988 | 0 | xmlFreeNode(cur); |
1989 | 0 | return(NULL); |
1990 | 5.60k | } |
1991 | | |
1992 | | /** |
1993 | | * xmlNewPI: |
1994 | | * @name: the processing instruction target |
1995 | | * @content: the PI content (optional) |
1996 | | * |
1997 | | * Create a processing instruction node. |
1998 | | * |
1999 | | * Use of this function is DISCOURAGED in favor of xmlNewDocPI. |
2000 | | * |
2001 | | * Returns a pointer to the new node object or NULL if arguments are |
2002 | | * invalid or a memory allocation failed. |
2003 | | */ |
2004 | | xmlNodePtr |
2005 | 0 | xmlNewPI(const xmlChar *name, const xmlChar *content) { |
2006 | 0 | return(xmlNewDocPI(NULL, name, content)); |
2007 | 0 | } |
2008 | | |
2009 | | /** |
2010 | | * xmlNewNode: |
2011 | | * @ns: namespace (optional) |
2012 | | * @name: the node name |
2013 | | * |
2014 | | * Create an element node. |
2015 | | * |
2016 | | * Use of this function is DISCOURAGED in favor of xmlNewDocNode. |
2017 | | * |
2018 | | * Returns a pointer to the new node object or NULL if arguments are |
2019 | | * invalid or a memory allocation failed. |
2020 | | */ |
2021 | | xmlNodePtr |
2022 | 0 | xmlNewNode(xmlNsPtr ns, const xmlChar *name) { |
2023 | 0 | return(xmlNewDocNode(NULL, ns, name, NULL)); |
2024 | 0 | } |
2025 | | |
2026 | | /** |
2027 | | * xmlNewNodeEatName: |
2028 | | * @ns: namespace (optional) |
2029 | | * @name: the node name |
2030 | | * |
2031 | | * Create an element node. |
2032 | | * |
2033 | | * Use of this function is DISCOURAGED in favor of xmlNewDocNodeEatName. |
2034 | | * |
2035 | | * Like xmlNewNode, but the @name string will be used directly |
2036 | | * without making a copy. Takes ownership of @name which will also |
2037 | | * be freed on error. |
2038 | | * |
2039 | | * Returns a pointer to the new node object or NULL if arguments are |
2040 | | * invalid or a memory allocation failed. |
2041 | | */ |
2042 | | xmlNodePtr |
2043 | 0 | xmlNewNodeEatName(xmlNsPtr ns, xmlChar *name) { |
2044 | 0 | return(xmlNewDocNodeEatName(NULL, ns, name, NULL)); |
2045 | 0 | } |
2046 | | |
2047 | | static xmlNodePtr |
2048 | | xmlNewElem(xmlDocPtr doc, xmlNsPtr ns, const xmlChar *name, |
2049 | 43.8k | const xmlChar *content) { |
2050 | 43.8k | xmlNodePtr cur; |
2051 | | |
2052 | 43.8k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2053 | 43.8k | if (cur == NULL) |
2054 | 0 | return(NULL); |
2055 | 43.8k | memset(cur, 0, sizeof(xmlNode)); |
2056 | | |
2057 | 43.8k | cur->type = XML_ELEMENT_NODE; |
2058 | 43.8k | cur->doc = doc; |
2059 | 43.8k | cur->name = name; |
2060 | 43.8k | cur->ns = ns; |
2061 | | |
2062 | 43.8k | if (content != NULL) { |
2063 | 0 | if (xmlNodeParseContent(cur, content, -1) < 0) { |
2064 | | /* Don't free name on error */ |
2065 | 0 | xmlFree(cur); |
2066 | 0 | return(NULL); |
2067 | 0 | } |
2068 | 0 | } |
2069 | | |
2070 | 43.8k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2071 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)cur); |
2072 | | |
2073 | 43.8k | return(cur); |
2074 | 43.8k | } |
2075 | | |
2076 | | /** |
2077 | | * xmlNewDocNode: |
2078 | | * @doc: the target document |
2079 | | * @ns: namespace (optional) |
2080 | | * @name: the node name |
2081 | | * @content: text content with XML references (optional) |
2082 | | * |
2083 | | * Create an element node. |
2084 | | * |
2085 | | * If provided, @content is expected to be a valid XML attribute value |
2086 | | * possibly containing character and entity references. Syntax errors |
2087 | | * and references to undeclared entities are ignored silently. |
2088 | | * Only references are handled, nested elements, comments or PIs are |
2089 | | * not. See xmlNewDocRawNode for an alternative. |
2090 | | * |
2091 | | * General notes on object creation: |
2092 | | * |
2093 | | * Each node and all its children are associated with the same |
2094 | | * document. The document should be provided when creating nodes to |
2095 | | * avoid a performance penalty when adding the node to a document |
2096 | | * tree. Note that a document only owns nodes reachable from the root |
2097 | | * node. Unlinked subtrees must be freed manually. |
2098 | | * |
2099 | | * Returns a pointer to the new node object or NULL if arguments are |
2100 | | * invalid or a memory allocation failed. |
2101 | | */ |
2102 | | xmlNodePtr |
2103 | | xmlNewDocNode(xmlDocPtr doc, xmlNsPtr ns, |
2104 | 863 | const xmlChar *name, const xmlChar *content) { |
2105 | 863 | xmlNodePtr cur; |
2106 | 863 | xmlChar *copy; |
2107 | | |
2108 | 863 | if (name == NULL) |
2109 | 0 | return(NULL); |
2110 | | |
2111 | 863 | if ((doc != NULL) && (doc->dict != NULL)) { |
2112 | 863 | const xmlChar *dictName = xmlDictLookup(doc->dict, name, -1); |
2113 | | |
2114 | 863 | if (dictName == NULL) |
2115 | 0 | return(NULL); |
2116 | 863 | return(xmlNewElem(doc, ns, dictName, content)); |
2117 | 863 | } |
2118 | | |
2119 | 0 | copy = xmlStrdup(name); |
2120 | 0 | if (copy == NULL) |
2121 | 0 | return(NULL); |
2122 | | |
2123 | 0 | cur = xmlNewElem(doc, ns, copy, content); |
2124 | 0 | if (cur == NULL) { |
2125 | 0 | xmlFree(copy); |
2126 | 0 | return(NULL); |
2127 | 0 | } |
2128 | | |
2129 | 0 | return(cur); |
2130 | 0 | } |
2131 | | |
2132 | | /** |
2133 | | * xmlNewDocNodeEatName: |
2134 | | * @doc: the target document |
2135 | | * @ns: namespace (optional) |
2136 | | * @name: the node name |
2137 | | * @content: text content with XML references (optional) |
2138 | | * |
2139 | | * Create an element node. |
2140 | | * |
2141 | | * Like xmlNewDocNode, but the @name string will be used directly |
2142 | | * without making a copy. Takes ownership of @name which will also |
2143 | | * be freed on error. |
2144 | | * |
2145 | | * Returns a pointer to the new node object or NULL if arguments are |
2146 | | * invalid or a memory allocation failed. |
2147 | | */ |
2148 | | xmlNodePtr |
2149 | | xmlNewDocNodeEatName(xmlDocPtr doc, xmlNsPtr ns, |
2150 | 43.0k | xmlChar *name, const xmlChar *content) { |
2151 | 43.0k | xmlNodePtr cur; |
2152 | | |
2153 | 43.0k | if (name == NULL) |
2154 | 0 | return(NULL); |
2155 | | |
2156 | 43.0k | cur = xmlNewElem(doc, ns, name, content); |
2157 | 43.0k | if (cur == NULL) { |
2158 | | /* if name doesn't come from the doc dictionary free it here */ |
2159 | 0 | if ((doc == NULL) || |
2160 | 0 | (doc->dict == NULL) || |
2161 | 0 | (!xmlDictOwns(doc->dict, name))) |
2162 | 0 | xmlFree(name); |
2163 | 0 | return(NULL); |
2164 | 0 | } |
2165 | | |
2166 | 43.0k | return(cur); |
2167 | 43.0k | } |
2168 | | |
2169 | | /** |
2170 | | * xmlNewDocRawNode: |
2171 | | * @doc: the target document |
2172 | | * @ns: namespace (optional) |
2173 | | * @name: the node name |
2174 | | * @content: raw text content (optional) |
2175 | | * |
2176 | | * Create an element node. |
2177 | | * |
2178 | | * If provided, @value should be a raw, unescaped string. |
2179 | | * |
2180 | | * Returns a pointer to the new node object or NULL if arguments are |
2181 | | * invalid or a memory allocation failed. |
2182 | | */ |
2183 | | xmlNodePtr |
2184 | | xmlNewDocRawNode(xmlDocPtr doc, xmlNsPtr ns, |
2185 | 0 | const xmlChar *name, const xmlChar *content) { |
2186 | 0 | xmlNodePtr cur; |
2187 | |
|
2188 | 0 | cur = xmlNewDocNode(doc, ns, name, NULL); |
2189 | 0 | if (cur != NULL) { |
2190 | 0 | cur->doc = doc; |
2191 | 0 | if (content != NULL) { |
2192 | 0 | xmlNodePtr text; |
2193 | |
|
2194 | 0 | text = xmlNewDocText(doc, content); |
2195 | 0 | if (text == NULL) { |
2196 | 0 | xmlFreeNode(cur); |
2197 | 0 | return(NULL); |
2198 | 0 | } |
2199 | | |
2200 | 0 | cur->children = text; |
2201 | 0 | cur->last = text; |
2202 | 0 | text->parent = cur; |
2203 | 0 | } |
2204 | 0 | } |
2205 | 0 | return(cur); |
2206 | 0 | } |
2207 | | |
2208 | | /** |
2209 | | * xmlNewDocFragment: |
2210 | | * @doc: the target document (optional) |
2211 | | * |
2212 | | * Create a document fragment node. |
2213 | | * |
2214 | | * Returns a pointer to the new node object or NULL if a memory |
2215 | | * allocation failed. |
2216 | | */ |
2217 | | xmlNodePtr |
2218 | 0 | xmlNewDocFragment(xmlDocPtr doc) { |
2219 | 0 | xmlNodePtr cur; |
2220 | | |
2221 | | /* |
2222 | | * Allocate a new DocumentFragment node and fill the fields. |
2223 | | */ |
2224 | 0 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2225 | 0 | if (cur == NULL) |
2226 | 0 | return(NULL); |
2227 | 0 | memset(cur, 0, sizeof(xmlNode)); |
2228 | 0 | cur->type = XML_DOCUMENT_FRAG_NODE; |
2229 | |
|
2230 | 0 | cur->doc = doc; |
2231 | |
|
2232 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2233 | 0 | xmlRegisterNodeDefaultValue(cur); |
2234 | 0 | return(cur); |
2235 | 0 | } |
2236 | | |
2237 | | /** |
2238 | | * xmlNewText: |
2239 | | * @content: raw text content (optional) |
2240 | | * |
2241 | | * Create a text node. |
2242 | | * |
2243 | | * Use of this function is DISCOURAGED in favor of xmlNewDocText. |
2244 | | * |
2245 | | * Returns a pointer to the new node object or NULL if a memory |
2246 | | * allocation failed. |
2247 | | */ |
2248 | | xmlNodePtr |
2249 | 5.90k | xmlNewText(const xmlChar *content) { |
2250 | 5.90k | xmlNodePtr cur; |
2251 | | |
2252 | | /* |
2253 | | * Allocate a new node and fill the fields. |
2254 | | */ |
2255 | 5.90k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2256 | 5.90k | if (cur == NULL) |
2257 | 0 | return(NULL); |
2258 | 5.90k | memset(cur, 0, sizeof(xmlNode)); |
2259 | 5.90k | cur->type = XML_TEXT_NODE; |
2260 | | |
2261 | 5.90k | cur->name = xmlStringText; |
2262 | 5.90k | if (content != NULL) { |
2263 | 0 | cur->content = xmlStrdup(content); |
2264 | 0 | if (cur->content == NULL) |
2265 | 0 | goto error; |
2266 | 0 | } |
2267 | | |
2268 | 5.90k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2269 | 0 | xmlRegisterNodeDefaultValue(cur); |
2270 | 5.90k | return(cur); |
2271 | | |
2272 | 0 | error: |
2273 | 0 | xmlFreeNode(cur); |
2274 | 0 | return(NULL); |
2275 | 5.90k | } |
2276 | | |
2277 | | /** |
2278 | | * xmlNewTextChild: |
2279 | | * @parent: the parent node |
2280 | | * @ns: a namespace (optional) |
2281 | | * @name: the name of the child |
2282 | | * @content: raw text content of the child (optional) |
2283 | | * |
2284 | | * Create a new child element and append it to a parent element. |
2285 | | * |
2286 | | * If @ns is NULL, the newly created element inherits the namespace |
2287 | | * of the parent. |
2288 | | * |
2289 | | * If @content is provided, a text node will be added to the child |
2290 | | * element, see xmlNewDocRawNode. |
2291 | | * |
2292 | | * Returns a pointer to the new node object or NULL if arguments |
2293 | | * are invalid or a memory allocation failed. |
2294 | | */ |
2295 | | xmlNodePtr |
2296 | | xmlNewTextChild(xmlNodePtr parent, xmlNsPtr ns, |
2297 | 0 | const xmlChar *name, const xmlChar *content) { |
2298 | 0 | xmlNodePtr cur, prev; |
2299 | |
|
2300 | 0 | if ((parent == NULL) || (name == NULL)) |
2301 | 0 | return(NULL); |
2302 | | |
2303 | 0 | switch (parent->type) { |
2304 | 0 | case XML_DOCUMENT_NODE: |
2305 | 0 | case XML_HTML_DOCUMENT_NODE: |
2306 | 0 | case XML_DOCUMENT_FRAG_NODE: |
2307 | 0 | break; |
2308 | | |
2309 | 0 | case XML_ELEMENT_NODE: |
2310 | 0 | if (ns == NULL) |
2311 | 0 | ns = parent->ns; |
2312 | 0 | break; |
2313 | | |
2314 | 0 | default: |
2315 | 0 | return(NULL); |
2316 | 0 | } |
2317 | | |
2318 | 0 | cur = xmlNewDocRawNode(parent->doc, ns, name, content); |
2319 | 0 | if (cur == NULL) |
2320 | 0 | return(NULL); |
2321 | | |
2322 | | /* |
2323 | | * add the new element at the end of the children list. |
2324 | | */ |
2325 | 0 | cur->parent = parent; |
2326 | 0 | if (parent->children == NULL) { |
2327 | 0 | parent->children = cur; |
2328 | 0 | parent->last = cur; |
2329 | 0 | } else { |
2330 | 0 | prev = parent->last; |
2331 | 0 | prev->next = cur; |
2332 | 0 | cur->prev = prev; |
2333 | 0 | parent->last = cur; |
2334 | 0 | } |
2335 | |
|
2336 | 0 | return(cur); |
2337 | 0 | } |
2338 | | |
2339 | | /** |
2340 | | * xmlNewEntityRef: |
2341 | | * @doc: the target document (optional) |
2342 | | * @name: the entity name |
2343 | | * |
2344 | | * Create an empty entity reference node. This function doesn't attempt |
2345 | | * to look up the entity in @doc. |
2346 | | * |
2347 | | * @name is consumed. |
2348 | | * |
2349 | | * Returns a pointer to the new node object or NULL if arguments are |
2350 | | * invalid or a memory allocation failed. |
2351 | | */ |
2352 | | static xmlNodePtr |
2353 | 4.53k | xmlNewEntityRef(xmlDocPtr doc, xmlChar *name) { |
2354 | 4.53k | xmlNodePtr cur; |
2355 | | |
2356 | | /* |
2357 | | * Allocate a new node and fill the fields. |
2358 | | */ |
2359 | 4.53k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2360 | 4.53k | if (cur == NULL) { |
2361 | 0 | xmlFree(name); |
2362 | 0 | return(NULL); |
2363 | 0 | } |
2364 | 4.53k | memset(cur, 0, sizeof(xmlNode)); |
2365 | 4.53k | cur->type = XML_ENTITY_REF_NODE; |
2366 | 4.53k | cur->doc = doc; |
2367 | 4.53k | cur->name = name; |
2368 | | |
2369 | 4.53k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2370 | 0 | xmlRegisterNodeDefaultValue(cur); |
2371 | | |
2372 | 4.53k | return(cur); |
2373 | 4.53k | } |
2374 | | |
2375 | | /** |
2376 | | * xmlNewCharRef: |
2377 | | * @doc: the target document (optional) |
2378 | | * @name: the entity name |
2379 | | * |
2380 | | * This function is MISNAMED. It doesn't create a character reference |
2381 | | * but an entity reference. |
2382 | | * |
2383 | | * Create an empty entity reference node. This function doesn't attempt |
2384 | | * to look up the entity in @doc. |
2385 | | * |
2386 | | * Entity names like '&entity;' are handled as well. |
2387 | | * |
2388 | | * Returns a pointer to the new node object or NULL if arguments are |
2389 | | * invalid or a memory allocation failed. |
2390 | | */ |
2391 | | xmlNodePtr |
2392 | 0 | xmlNewCharRef(xmlDocPtr doc, const xmlChar *name) { |
2393 | 0 | xmlChar *copy; |
2394 | |
|
2395 | 0 | if (name == NULL) |
2396 | 0 | return(NULL); |
2397 | | |
2398 | 0 | if (name[0] == '&') { |
2399 | 0 | int len; |
2400 | 0 | name++; |
2401 | 0 | len = xmlStrlen(name); |
2402 | 0 | if (name[len - 1] == ';') |
2403 | 0 | copy = xmlStrndup(name, len - 1); |
2404 | 0 | else |
2405 | 0 | copy = xmlStrndup(name, len); |
2406 | 0 | } else |
2407 | 0 | copy = xmlStrdup(name); |
2408 | 0 | if (copy == NULL) |
2409 | 0 | return(NULL); |
2410 | | |
2411 | 0 | return(xmlNewEntityRef(doc, copy)); |
2412 | 0 | } |
2413 | | |
2414 | | /** |
2415 | | * xmlNewReference: |
2416 | | * @doc: the target document (optional) |
2417 | | * @name: the entity name |
2418 | | * |
2419 | | * Create a new entity reference node, linking the result with the |
2420 | | * entity in @doc if found. |
2421 | | * |
2422 | | * Entity names like '&entity;' are handled as well. |
2423 | | * |
2424 | | * Returns a pointer to the new node object or NULL if arguments are |
2425 | | * invalid or a memory allocation failed. |
2426 | | */ |
2427 | | xmlNodePtr |
2428 | 2.28k | xmlNewReference(const xmlDoc *doc, const xmlChar *name) { |
2429 | 2.28k | xmlNodePtr cur; |
2430 | 2.28k | xmlEntityPtr ent; |
2431 | | |
2432 | 2.28k | if (name == NULL) |
2433 | 0 | return(NULL); |
2434 | | |
2435 | | /* |
2436 | | * Allocate a new node and fill the fields. |
2437 | | */ |
2438 | 2.28k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2439 | 2.28k | if (cur == NULL) |
2440 | 0 | return(NULL); |
2441 | 2.28k | memset(cur, 0, sizeof(xmlNode)); |
2442 | 2.28k | cur->type = XML_ENTITY_REF_NODE; |
2443 | | |
2444 | 2.28k | cur->doc = (xmlDoc *)doc; |
2445 | 2.28k | if (name[0] == '&') { |
2446 | 0 | int len; |
2447 | 0 | name++; |
2448 | 0 | len = xmlStrlen(name); |
2449 | 0 | if (name[len - 1] == ';') |
2450 | 0 | cur->name = xmlStrndup(name, len - 1); |
2451 | 0 | else |
2452 | 0 | cur->name = xmlStrndup(name, len); |
2453 | 0 | } else |
2454 | 2.28k | cur->name = xmlStrdup(name); |
2455 | 2.28k | if (cur->name == NULL) |
2456 | 0 | goto error; |
2457 | | |
2458 | 2.28k | ent = xmlGetDocEntity(doc, cur->name); |
2459 | 2.28k | if (ent != NULL) { |
2460 | 1.83k | cur->content = ent->content; |
2461 | | /* |
2462 | | * The parent pointer in entity is a DTD pointer and thus is NOT |
2463 | | * updated. Not sure if this is 100% correct. |
2464 | | * -George |
2465 | | */ |
2466 | 1.83k | cur->children = (xmlNodePtr) ent; |
2467 | 1.83k | cur->last = (xmlNodePtr) ent; |
2468 | 1.83k | } |
2469 | | |
2470 | 2.28k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2471 | 0 | xmlRegisterNodeDefaultValue(cur); |
2472 | 2.28k | return(cur); |
2473 | | |
2474 | 0 | error: |
2475 | 0 | xmlFreeNode(cur); |
2476 | 0 | return(NULL); |
2477 | 2.28k | } |
2478 | | |
2479 | | /** |
2480 | | * xmlNewDocText: |
2481 | | * @doc: the target document |
2482 | | * @content: raw text content (optional) |
2483 | | * |
2484 | | * Create a new text node. |
2485 | | * |
2486 | | * Returns a pointer to the new node object or NULL if a memory |
2487 | | * allocation failed. |
2488 | | */ |
2489 | | xmlNodePtr |
2490 | 5.90k | xmlNewDocText(const xmlDoc *doc, const xmlChar *content) { |
2491 | 5.90k | xmlNodePtr cur; |
2492 | | |
2493 | 5.90k | cur = xmlNewText(content); |
2494 | 5.90k | if (cur != NULL) cur->doc = (xmlDoc *)doc; |
2495 | 5.90k | return(cur); |
2496 | 5.90k | } |
2497 | | |
2498 | | /** |
2499 | | * xmlNewTextLen: |
2500 | | * @content: raw text content (optional) |
2501 | | * @len: size of text content |
2502 | | * |
2503 | | * Use of this function is DISCOURAGED in favor of xmlNewDocTextLen. |
2504 | | * |
2505 | | * Returns a pointer to the new node object or NULL if a memory |
2506 | | * allocation failed. |
2507 | | */ |
2508 | | xmlNodePtr |
2509 | 0 | xmlNewTextLen(const xmlChar *content, int len) { |
2510 | 0 | xmlNodePtr cur; |
2511 | | |
2512 | | /* |
2513 | | * Allocate a new node and fill the fields. |
2514 | | */ |
2515 | 0 | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2516 | 0 | if (cur == NULL) |
2517 | 0 | return(NULL); |
2518 | 0 | memset(cur, 0, sizeof(xmlNode)); |
2519 | 0 | cur->type = XML_TEXT_NODE; |
2520 | |
|
2521 | 0 | cur->name = xmlStringText; |
2522 | 0 | if (content != NULL) { |
2523 | 0 | cur->content = xmlStrndup(content, len); |
2524 | 0 | if (cur->content == NULL) { |
2525 | 0 | xmlFreeNode(cur); |
2526 | 0 | return(NULL); |
2527 | 0 | } |
2528 | 0 | } |
2529 | | |
2530 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2531 | 0 | xmlRegisterNodeDefaultValue(cur); |
2532 | 0 | return(cur); |
2533 | 0 | } |
2534 | | |
2535 | | /** |
2536 | | * xmlNewDocTextLen: |
2537 | | * @doc: the target document |
2538 | | * @content: raw text content (optional) |
2539 | | * @len: size of text content |
2540 | | * |
2541 | | * Create a new text node. |
2542 | | * |
2543 | | * Returns a pointer to the new node object or NULL if a memory |
2544 | | * allocation failed. |
2545 | | */ |
2546 | | xmlNodePtr |
2547 | 0 | xmlNewDocTextLen(xmlDocPtr doc, const xmlChar *content, int len) { |
2548 | 0 | xmlNodePtr cur; |
2549 | |
|
2550 | 0 | cur = xmlNewTextLen(content, len); |
2551 | 0 | if (cur != NULL) cur->doc = doc; |
2552 | 0 | return(cur); |
2553 | 0 | } |
2554 | | |
2555 | | /** |
2556 | | * xmlNewComment: |
2557 | | * @content: the comment content (optional) |
2558 | | * |
2559 | | * Use of this function is DISCOURAGED in favor of xmlNewDocComment. |
2560 | | * |
2561 | | * Create a comment node. |
2562 | | * |
2563 | | * Returns a pointer to the new node object or NULL if a memory |
2564 | | * allocation failed. |
2565 | | */ |
2566 | | xmlNodePtr |
2567 | 2.70k | xmlNewComment(const xmlChar *content) { |
2568 | 2.70k | xmlNodePtr cur; |
2569 | | |
2570 | | /* |
2571 | | * Allocate a new node and fill the fields. |
2572 | | */ |
2573 | 2.70k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2574 | 2.70k | if (cur == NULL) |
2575 | 0 | return(NULL); |
2576 | 2.70k | memset(cur, 0, sizeof(xmlNode)); |
2577 | 2.70k | cur->type = XML_COMMENT_NODE; |
2578 | | |
2579 | 2.70k | cur->name = xmlStringComment; |
2580 | 2.70k | if (content != NULL) { |
2581 | 2.70k | cur->content = xmlStrdup(content); |
2582 | 2.70k | if (cur->content == NULL) |
2583 | 0 | goto error; |
2584 | 2.70k | } |
2585 | | |
2586 | 2.70k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2587 | 0 | xmlRegisterNodeDefaultValue(cur); |
2588 | 2.70k | return(cur); |
2589 | | |
2590 | 0 | error: |
2591 | 0 | xmlFreeNode(cur); |
2592 | 0 | return(NULL); |
2593 | 2.70k | } |
2594 | | |
2595 | | /** |
2596 | | * xmlNewCDataBlock: |
2597 | | * @doc: the target document (optional) |
2598 | | * @content: raw text content (optional) |
2599 | | * @len: size of text content |
2600 | | * |
2601 | | * Create a CDATA section node. |
2602 | | * |
2603 | | * Returns a pointer to the new node object or NULL if a memory |
2604 | | * allocation failed. |
2605 | | */ |
2606 | | xmlNodePtr |
2607 | 2.36k | xmlNewCDataBlock(xmlDocPtr doc, const xmlChar *content, int len) { |
2608 | 2.36k | xmlNodePtr cur; |
2609 | | |
2610 | | /* |
2611 | | * Allocate a new node and fill the fields. |
2612 | | */ |
2613 | 2.36k | cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
2614 | 2.36k | if (cur == NULL) |
2615 | 0 | return(NULL); |
2616 | 2.36k | memset(cur, 0, sizeof(xmlNode)); |
2617 | 2.36k | cur->type = XML_CDATA_SECTION_NODE; |
2618 | 2.36k | cur->doc = doc; |
2619 | | |
2620 | 2.36k | if (content != NULL) { |
2621 | 2.36k | cur->content = xmlStrndup(content, len); |
2622 | 2.36k | if (cur->content == NULL) { |
2623 | 0 | xmlFree(cur); |
2624 | 0 | return(NULL); |
2625 | 0 | } |
2626 | 2.36k | } |
2627 | | |
2628 | 2.36k | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
2629 | 0 | xmlRegisterNodeDefaultValue(cur); |
2630 | 2.36k | return(cur); |
2631 | 2.36k | } |
2632 | | |
2633 | | /** |
2634 | | * xmlNewDocComment: |
2635 | | * @doc: the document |
2636 | | * @content: the comment content |
2637 | | * |
2638 | | * Create a comment node. |
2639 | | * |
2640 | | * Returns a pointer to the new node object or NULL if a memory |
2641 | | * allocation failed. |
2642 | | */ |
2643 | | xmlNodePtr |
2644 | 2.70k | xmlNewDocComment(xmlDocPtr doc, const xmlChar *content) { |
2645 | 2.70k | xmlNodePtr cur; |
2646 | | |
2647 | 2.70k | cur = xmlNewComment(content); |
2648 | 2.70k | if (cur != NULL) cur->doc = doc; |
2649 | 2.70k | return(cur); |
2650 | 2.70k | } |
2651 | | |
2652 | | static void |
2653 | 0 | xmlRemoveEntity(xmlEntityPtr ent) { |
2654 | 0 | xmlDocPtr doc = ent->doc; |
2655 | 0 | xmlDtdPtr intSubset, extSubset; |
2656 | |
|
2657 | 0 | if (doc == NULL) |
2658 | 0 | return; |
2659 | 0 | intSubset = doc->intSubset; |
2660 | 0 | extSubset = doc->extSubset; |
2661 | |
|
2662 | 0 | if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
2663 | 0 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
2664 | 0 | (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) { |
2665 | 0 | if (intSubset != NULL) { |
2666 | 0 | if (xmlHashLookup(intSubset->entities, ent->name) == ent) |
2667 | 0 | xmlHashRemoveEntry(intSubset->entities, ent->name, NULL); |
2668 | 0 | } |
2669 | 0 | if (extSubset != NULL) { |
2670 | 0 | if (xmlHashLookup(extSubset->entities, ent->name) == ent) |
2671 | 0 | xmlHashRemoveEntry(extSubset->entities, ent->name, NULL); |
2672 | 0 | } |
2673 | 0 | } else if ((ent->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
2674 | 0 | (ent->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
2675 | 0 | if (intSubset != NULL) { |
2676 | 0 | if (xmlHashLookup(intSubset->pentities, ent->name) == ent) |
2677 | 0 | xmlHashRemoveEntry(intSubset->entities, ent->name, NULL); |
2678 | 0 | } |
2679 | 0 | if (extSubset != NULL) { |
2680 | 0 | if (xmlHashLookup(extSubset->pentities, ent->name) == ent) |
2681 | 0 | xmlHashRemoveEntry(extSubset->entities, ent->name, NULL); |
2682 | 0 | } |
2683 | 0 | } |
2684 | 0 | } |
2685 | | |
2686 | | static int |
2687 | 0 | xmlNodeSetDoc(xmlNodePtr node, xmlDocPtr doc) { |
2688 | 0 | xmlDocPtr oldDoc; |
2689 | 0 | xmlDictPtr oldDict, newDict; |
2690 | 0 | int ret = 0; |
2691 | | |
2692 | | /* |
2693 | | * Remove name and content from old dictionary |
2694 | | */ |
2695 | |
|
2696 | 0 | oldDoc = node->doc; |
2697 | 0 | oldDict = oldDoc ? oldDoc->dict : NULL; |
2698 | 0 | newDict = doc ? doc->dict : NULL; |
2699 | |
|
2700 | 0 | if ((oldDict != NULL) && (oldDict != newDict)) { |
2701 | 0 | if ((node->name != NULL) && |
2702 | 0 | ((node->type == XML_ELEMENT_NODE) || |
2703 | 0 | (node->type == XML_ATTRIBUTE_NODE) || |
2704 | 0 | (node->type == XML_PI_NODE) || |
2705 | 0 | (node->type == XML_ENTITY_REF_NODE)) && |
2706 | 0 | (xmlDictOwns(oldDict, node->name))) { |
2707 | 0 | if (newDict) |
2708 | 0 | node->name = xmlDictLookup(newDict, node->name, -1); |
2709 | 0 | else |
2710 | 0 | node->name = xmlStrdup(node->name); |
2711 | 0 | if (node->name == NULL) |
2712 | 0 | ret = -1; |
2713 | 0 | } |
2714 | |
|
2715 | 0 | if ((node->content != NULL) && |
2716 | 0 | ((node->type == XML_TEXT_NODE) || |
2717 | 0 | (node->type == XML_CDATA_SECTION_NODE)) && |
2718 | 0 | (xmlDictOwns(oldDict, node->content))) { |
2719 | 0 | node->content = xmlStrdup(node->content); |
2720 | 0 | if (node->content == NULL) |
2721 | 0 | ret = -1; |
2722 | 0 | } |
2723 | 0 | } |
2724 | |
|
2725 | 0 | switch (node->type) { |
2726 | 0 | case XML_ATTRIBUTE_NODE: { |
2727 | 0 | xmlAttrPtr attr = (xmlAttrPtr) node; |
2728 | | |
2729 | | /* |
2730 | | * Handle IDs |
2731 | | * |
2732 | | * TODO: ID attributes should also be added to the new |
2733 | | * document, but it's not clear how to handle clashes. |
2734 | | */ |
2735 | 0 | if (attr->atype == XML_ATTRIBUTE_ID) |
2736 | 0 | xmlRemoveID(oldDoc, attr); |
2737 | |
|
2738 | 0 | break; |
2739 | 0 | } |
2740 | | |
2741 | 0 | case XML_ENTITY_REF_NODE: |
2742 | | /* |
2743 | | * Handle entity references |
2744 | | */ |
2745 | 0 | node->children = NULL; |
2746 | 0 | node->last = NULL; |
2747 | 0 | node->content = NULL; |
2748 | |
|
2749 | 0 | if ((doc != NULL) && |
2750 | 0 | ((doc->intSubset != NULL) || (doc->extSubset != NULL))) { |
2751 | 0 | xmlEntityPtr ent; |
2752 | | |
2753 | | /* |
2754 | | * Assign new entity node if available |
2755 | | */ |
2756 | 0 | ent = xmlGetDocEntity(doc, node->name); |
2757 | 0 | if (ent != NULL) { |
2758 | 0 | node->children = (xmlNodePtr) ent; |
2759 | 0 | node->last = (xmlNodePtr) ent; |
2760 | 0 | node->content = ent->content; |
2761 | 0 | } |
2762 | 0 | } |
2763 | |
|
2764 | 0 | break; |
2765 | | |
2766 | 0 | case XML_DTD_NODE: |
2767 | 0 | if (oldDoc != NULL) { |
2768 | 0 | if (oldDoc->intSubset == (xmlDtdPtr) node) |
2769 | 0 | oldDoc->intSubset = NULL; |
2770 | 0 | if (oldDoc->extSubset == (xmlDtdPtr) node) |
2771 | 0 | oldDoc->extSubset = NULL; |
2772 | 0 | } |
2773 | |
|
2774 | 0 | break; |
2775 | | |
2776 | 0 | case XML_ENTITY_DECL: |
2777 | 0 | xmlRemoveEntity((xmlEntityPtr) node); |
2778 | 0 | break; |
2779 | | |
2780 | | /* |
2781 | | * TODO: |
2782 | | * - Remove element decls from doc->elements |
2783 | | * - Remove attribtue decls form doc->attributes |
2784 | | */ |
2785 | | |
2786 | 0 | default: |
2787 | 0 | break; |
2788 | 0 | } |
2789 | | |
2790 | | /* |
2791 | | * Set new document |
2792 | | */ |
2793 | 0 | node->doc = doc; |
2794 | |
|
2795 | 0 | return(ret); |
2796 | 0 | } |
2797 | | |
2798 | | /** |
2799 | | * xmlSetTreeDoc: |
2800 | | * @tree: root of a subtree |
2801 | | * @doc: new document |
2802 | | * |
2803 | | * This is an internal function which shouldn't be used. It is |
2804 | | * invoked by functions like xmlAddChild, xmlAddSibling or |
2805 | | * xmlReplaceNode. @tree must be the root node of an unlinked |
2806 | | * subtree. |
2807 | | * |
2808 | | * Associate all nodes in a tree with a new document. |
2809 | | * |
2810 | | * Also copy strings from the old document's dictionary and |
2811 | | * remove ID attributes from the old ID table. |
2812 | | * |
2813 | | * Returns 0 on success. If a memory allocation fails, returns -1. |
2814 | | * The whole tree will be updated on failure but some strings |
2815 | | * may be lost. |
2816 | | */ |
2817 | | int |
2818 | 0 | xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { |
2819 | 0 | int ret = 0; |
2820 | |
|
2821 | 0 | if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL)) |
2822 | 0 | return(0); |
2823 | 0 | if (tree->doc == doc) |
2824 | 0 | return(0); |
2825 | | |
2826 | 0 | if (tree->type == XML_ELEMENT_NODE) { |
2827 | 0 | xmlAttrPtr prop = tree->properties; |
2828 | |
|
2829 | 0 | while (prop != NULL) { |
2830 | 0 | if (prop->children != NULL) { |
2831 | 0 | if (xmlSetListDoc(prop->children, doc) < 0) |
2832 | 0 | ret = -1; |
2833 | 0 | } |
2834 | |
|
2835 | 0 | if (xmlNodeSetDoc((xmlNodePtr) prop, doc) < 0) |
2836 | 0 | ret = -1; |
2837 | |
|
2838 | 0 | prop = prop->next; |
2839 | 0 | } |
2840 | 0 | } |
2841 | |
|
2842 | 0 | if ((tree->children != NULL) && |
2843 | 0 | (tree->type != XML_ENTITY_REF_NODE)) { |
2844 | 0 | if (xmlSetListDoc(tree->children, doc) < 0) |
2845 | 0 | ret = -1; |
2846 | 0 | } |
2847 | |
|
2848 | 0 | if (xmlNodeSetDoc(tree, doc) < 0) |
2849 | 0 | ret = -1; |
2850 | |
|
2851 | 0 | return(ret); |
2852 | 0 | } |
2853 | | |
2854 | | /** |
2855 | | * xmlSetListDoc: |
2856 | | * @list: a node list |
2857 | | * @doc: new document |
2858 | | * |
2859 | | * Associate all subtrees in @list with a new document. |
2860 | | * |
2861 | | * Internal function, see xmlSetTreeDoc. |
2862 | | * |
2863 | | * Returns 0 on success. If a memory allocation fails, returns -1. |
2864 | | * All subtrees will be updated on failure but some strings |
2865 | | * may be lost. |
2866 | | */ |
2867 | | int |
2868 | 0 | xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { |
2869 | 0 | xmlNodePtr cur; |
2870 | 0 | int ret = 0; |
2871 | |
|
2872 | 0 | if ((list == NULL) || (list->type == XML_NAMESPACE_DECL)) |
2873 | 0 | return(0); |
2874 | | |
2875 | 0 | cur = list; |
2876 | 0 | while (cur != NULL) { |
2877 | 0 | if (cur->doc != doc) { |
2878 | 0 | if (xmlSetTreeDoc(cur, doc) < 0) |
2879 | 0 | ret = -1; |
2880 | 0 | } |
2881 | 0 | cur = cur->next; |
2882 | 0 | } |
2883 | |
|
2884 | 0 | return(ret); |
2885 | 0 | } |
2886 | | |
2887 | | /** |
2888 | | * xmlNewChild: |
2889 | | * @parent: the parent node |
2890 | | * @ns: a namespace (optional) |
2891 | | * @name: the name of the child |
2892 | | * @content: text content with XML references (optional) |
2893 | | * |
2894 | | * Create a new child element and append it to a parent element. |
2895 | | * |
2896 | | * If @ns is NULL, the newly created element inherits the namespace |
2897 | | * of the parent. |
2898 | | * |
2899 | | * If provided, @content is expected to be a valid XML attribute |
2900 | | * value possibly containing character and entity references. Text |
2901 | | * and entity reference node will be added to the child element, |
2902 | | * see xmlNewDocNode. |
2903 | | * |
2904 | | * Returns a pointer to the new node object or NULL if arguments |
2905 | | * are invalid or a memory allocation failed. |
2906 | | */ |
2907 | | xmlNodePtr |
2908 | | xmlNewChild(xmlNodePtr parent, xmlNsPtr ns, |
2909 | 0 | const xmlChar *name, const xmlChar *content) { |
2910 | 0 | xmlNodePtr cur, prev; |
2911 | |
|
2912 | 0 | if ((parent == NULL) || (name == NULL)) |
2913 | 0 | return(NULL); |
2914 | | |
2915 | 0 | switch (parent->type) { |
2916 | 0 | case XML_DOCUMENT_NODE: |
2917 | 0 | case XML_HTML_DOCUMENT_NODE: |
2918 | 0 | case XML_DOCUMENT_FRAG_NODE: |
2919 | 0 | break; |
2920 | | |
2921 | 0 | case XML_ELEMENT_NODE: |
2922 | 0 | if (ns == NULL) |
2923 | 0 | ns = parent->ns; |
2924 | 0 | break; |
2925 | | |
2926 | 0 | default: |
2927 | 0 | return(NULL); |
2928 | 0 | } |
2929 | | |
2930 | 0 | cur = xmlNewDocNode(parent->doc, ns, name, content); |
2931 | 0 | if (cur == NULL) |
2932 | 0 | return(NULL); |
2933 | | |
2934 | | /* |
2935 | | * add the new element at the end of the children list. |
2936 | | */ |
2937 | 0 | cur->parent = parent; |
2938 | 0 | if (parent->children == NULL) { |
2939 | 0 | parent->children = cur; |
2940 | 0 | parent->last = cur; |
2941 | 0 | } else { |
2942 | 0 | prev = parent->last; |
2943 | 0 | prev->next = cur; |
2944 | 0 | cur->prev = prev; |
2945 | 0 | parent->last = cur; |
2946 | 0 | } |
2947 | |
|
2948 | 0 | return(cur); |
2949 | 0 | } |
2950 | | |
2951 | | static void |
2952 | 0 | xmlTextSetContent(xmlNodePtr text, xmlChar *content) { |
2953 | 0 | if ((text->content != NULL) && |
2954 | 0 | (text->content != (xmlChar *) &text->properties)) { |
2955 | 0 | xmlDocPtr doc = text->doc; |
2956 | |
|
2957 | 0 | if ((doc == NULL) || |
2958 | 0 | (doc->dict == NULL) || |
2959 | 0 | (!xmlDictOwns(doc->dict, text->content))) |
2960 | 0 | xmlFree(text->content); |
2961 | 0 | } |
2962 | |
|
2963 | 0 | text->content = content; |
2964 | 0 | text->properties = NULL; |
2965 | 0 | } |
2966 | | |
2967 | | static int |
2968 | 0 | xmlTextAddContent(xmlNodePtr text, const xmlChar *content, int len) { |
2969 | 0 | xmlChar *merged; |
2970 | |
|
2971 | 0 | if (content == NULL) |
2972 | 0 | return(0); |
2973 | | |
2974 | 0 | merged = xmlStrncatNew(text->content, content, len); |
2975 | 0 | if (merged == NULL) |
2976 | 0 | return(-1); |
2977 | | |
2978 | 0 | xmlTextSetContent(text, merged); |
2979 | 0 | return(0); |
2980 | 0 | } |
2981 | | |
2982 | | static xmlNodePtr |
2983 | | xmlInsertProp(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent, |
2984 | 0 | xmlNodePtr prev, xmlNodePtr next) { |
2985 | 0 | xmlAttrPtr attr; |
2986 | |
|
2987 | 0 | if (((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE)) || |
2988 | 0 | ((next != NULL) && (next->type != XML_ATTRIBUTE_NODE))) |
2989 | 0 | return(NULL); |
2990 | | |
2991 | | /* check if an attribute with the same name exists */ |
2992 | 0 | attr = xmlGetPropNodeInternal(parent, cur->name, |
2993 | 0 | cur->ns ? cur->ns->href : NULL, 0); |
2994 | |
|
2995 | 0 | xmlUnlinkNodeInternal(cur); |
2996 | |
|
2997 | 0 | if (cur->doc != doc) { |
2998 | 0 | if (xmlSetTreeDoc(cur, doc) < 0) |
2999 | 0 | return(NULL); |
3000 | 0 | } |
3001 | | |
3002 | 0 | cur->parent = parent; |
3003 | 0 | cur->prev = prev; |
3004 | 0 | cur->next = next; |
3005 | |
|
3006 | 0 | if (prev == NULL) { |
3007 | 0 | if (parent != NULL) |
3008 | 0 | parent->properties = (xmlAttrPtr) cur; |
3009 | 0 | } else { |
3010 | 0 | prev->next = cur; |
3011 | 0 | } |
3012 | 0 | if (next != NULL) { |
3013 | 0 | next->prev = cur; |
3014 | 0 | } |
3015 | |
|
3016 | 0 | if ((attr != NULL) && (attr != (xmlAttrPtr) cur)) { |
3017 | | /* different instance, destroy it (attributes must be unique) */ |
3018 | 0 | xmlRemoveProp((xmlAttrPtr) attr); |
3019 | 0 | } |
3020 | |
|
3021 | 0 | return cur; |
3022 | 0 | } |
3023 | | |
3024 | | static xmlNodePtr |
3025 | | xmlInsertNode(xmlDocPtr doc, xmlNodePtr cur, xmlNodePtr parent, |
3026 | 0 | xmlNodePtr prev, xmlNodePtr next, int coalesce) { |
3027 | 0 | xmlNodePtr oldParent; |
3028 | |
|
3029 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) |
3030 | 0 | return xmlInsertProp(doc, cur, parent, prev, next); |
3031 | | |
3032 | | /* |
3033 | | * Coalesce text nodes |
3034 | | */ |
3035 | 0 | if ((coalesce) && (cur->type == XML_TEXT_NODE)) { |
3036 | 0 | if ((prev != NULL) && (prev->type == XML_TEXT_NODE) && |
3037 | 0 | (prev->name == cur->name)) { |
3038 | 0 | if (xmlTextAddContent(prev, cur->content, -1) < 0) |
3039 | 0 | return(NULL); |
3040 | 0 | xmlUnlinkNodeInternal(cur); |
3041 | 0 | xmlFreeNode(cur); |
3042 | 0 | return(prev); |
3043 | 0 | } |
3044 | | |
3045 | 0 | if ((next != NULL) && (next->type == XML_TEXT_NODE) && |
3046 | 0 | (next->name == cur->name)) { |
3047 | 0 | if (cur->content != NULL) { |
3048 | 0 | xmlChar *merged; |
3049 | |
|
3050 | 0 | merged = xmlStrncatNew(cur->content, next->content, -1); |
3051 | 0 | if (merged == NULL) |
3052 | 0 | return(NULL); |
3053 | 0 | xmlTextSetContent(next, merged); |
3054 | 0 | } |
3055 | | |
3056 | 0 | xmlUnlinkNodeInternal(cur); |
3057 | 0 | xmlFreeNode(cur); |
3058 | 0 | return(next); |
3059 | 0 | } |
3060 | 0 | } |
3061 | | |
3062 | | /* Unlink */ |
3063 | 0 | oldParent = cur->parent; |
3064 | 0 | if (oldParent != NULL) { |
3065 | 0 | if (oldParent->children == cur) |
3066 | 0 | oldParent->children = cur->next; |
3067 | 0 | if (oldParent->last == cur) |
3068 | 0 | oldParent->last = cur->prev; |
3069 | 0 | } |
3070 | 0 | if (cur->next != NULL) |
3071 | 0 | cur->next->prev = cur->prev; |
3072 | 0 | if (cur->prev != NULL) |
3073 | 0 | cur->prev->next = cur->next; |
3074 | |
|
3075 | 0 | if (cur->doc != doc) { |
3076 | 0 | if (xmlSetTreeDoc(cur, doc) < 0) { |
3077 | | /* |
3078 | | * We shouldn't make any modifications to the inserted |
3079 | | * tree if a memory allocation fails, but that's hard to |
3080 | | * implement. The tree has been moved to the target |
3081 | | * document now but some contents are corrupted. |
3082 | | * Unlinking is the best we can do. |
3083 | | */ |
3084 | 0 | cur->parent = NULL; |
3085 | 0 | cur->prev = NULL; |
3086 | 0 | cur->next = NULL; |
3087 | 0 | return(NULL); |
3088 | 0 | } |
3089 | 0 | } |
3090 | | |
3091 | 0 | cur->parent = parent; |
3092 | 0 | cur->prev = prev; |
3093 | 0 | cur->next = next; |
3094 | |
|
3095 | 0 | if (prev == NULL) { |
3096 | 0 | if (parent != NULL) |
3097 | 0 | parent->children = cur; |
3098 | 0 | } else { |
3099 | 0 | prev->next = cur; |
3100 | 0 | } |
3101 | 0 | if (next == NULL) { |
3102 | 0 | if (parent != NULL) |
3103 | 0 | parent->last = cur; |
3104 | 0 | } else { |
3105 | 0 | next->prev = cur; |
3106 | 0 | } |
3107 | |
|
3108 | 0 | return(cur); |
3109 | 0 | } |
3110 | | |
3111 | | /** |
3112 | | * xmlAddNextSibling: |
3113 | | * @prev: the target node |
3114 | | * @cur: the new node |
3115 | | * |
3116 | | * Unlinks @cur and inserts it as next sibling after @prev. |
3117 | | * |
3118 | | * Unlike xmlAddChild this function does not merge text nodes. |
3119 | | * |
3120 | | * If @cur is an attribute node, it is inserted after attribute |
3121 | | * @prev. If the attribute list contains an attribute with a name |
3122 | | * matching @cur, the old attribute is destroyed. |
3123 | | * |
3124 | | * See the notes in xmlAddChild. |
3125 | | * |
3126 | | * Returns @cur or a sibling if @cur was merged. Returns NULL |
3127 | | * if arguments are invalid or a memory allocation failed. |
3128 | | */ |
3129 | | xmlNodePtr |
3130 | 0 | xmlAddNextSibling(xmlNodePtr prev, xmlNodePtr cur) { |
3131 | 0 | if ((prev == NULL) || (prev->type == XML_NAMESPACE_DECL) || |
3132 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
3133 | 0 | (cur == prev)) |
3134 | 0 | return(NULL); |
3135 | | |
3136 | 0 | if (cur == prev->next) |
3137 | 0 | return(cur); |
3138 | | |
3139 | 0 | return(xmlInsertNode(prev->doc, cur, prev->parent, prev, prev->next, 0)); |
3140 | 0 | } |
3141 | | |
3142 | | /** |
3143 | | * xmlAddPrevSibling: |
3144 | | * @next: the target node |
3145 | | * @cur: the new node |
3146 | | * |
3147 | | * Unlinks @cur and inserts it as previous sibling before @next. |
3148 | | * |
3149 | | * Unlike xmlAddChild this function does not merge text nodes. |
3150 | | * |
3151 | | * If @cur is an attribute node, it is inserted before attribute |
3152 | | * @next. If the attribute list contains an attribute with a name |
3153 | | * matching @cur, the old attribute is destroyed. |
3154 | | * |
3155 | | * See the notes in xmlAddChild. |
3156 | | * |
3157 | | * Returns @cur or a sibling if @cur was merged. Returns NULL |
3158 | | * if arguments are invalid or a memory allocation failed. |
3159 | | */ |
3160 | | xmlNodePtr |
3161 | 0 | xmlAddPrevSibling(xmlNodePtr next, xmlNodePtr cur) { |
3162 | 0 | if ((next == NULL) || (next->type == XML_NAMESPACE_DECL) || |
3163 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
3164 | 0 | (cur == next)) |
3165 | 0 | return(NULL); |
3166 | | |
3167 | 0 | if (cur == next->prev) |
3168 | 0 | return(cur); |
3169 | | |
3170 | 0 | return(xmlInsertNode(next->doc, cur, next->parent, next->prev, next, 0)); |
3171 | 0 | } |
3172 | | |
3173 | | /** |
3174 | | * xmlAddSibling: |
3175 | | * @node: the target node |
3176 | | * @cur: the new node |
3177 | | * |
3178 | | * Unlinks @cur and inserts it as last sibling of @node. |
3179 | | * |
3180 | | * If @cur is a text node, it may be merged with an adjacent text |
3181 | | * node and freed. In this case the text node containing the merged |
3182 | | * content is returned. |
3183 | | * |
3184 | | * If @cur is an attribute node, it is appended to the attribute |
3185 | | * list containing @node. If the attribute list contains an attribute |
3186 | | * with a name matching @cur, the old attribute is destroyed. |
3187 | | * |
3188 | | * See the notes in xmlAddChild. |
3189 | | * |
3190 | | * Returns @cur or a sibling if @cur was merged. Returns NULL |
3191 | | * if arguments are invalid or a memory allocation failed. |
3192 | | */ |
3193 | | xmlNodePtr |
3194 | 0 | xmlAddSibling(xmlNodePtr node, xmlNodePtr cur) { |
3195 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
3196 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
3197 | 0 | (cur == node)) |
3198 | 0 | return(NULL); |
3199 | | |
3200 | | /* |
3201 | | * Constant time is we can rely on the ->parent->last to find |
3202 | | * the last sibling. |
3203 | | */ |
3204 | 0 | if ((node->type != XML_ATTRIBUTE_NODE) && (node->parent != NULL)) { |
3205 | 0 | if (node->parent->last != NULL) |
3206 | 0 | node = node->parent->last; |
3207 | 0 | } else { |
3208 | 0 | while (node->next != NULL) |
3209 | 0 | node = node->next; |
3210 | 0 | } |
3211 | |
|
3212 | 0 | if (cur == node) |
3213 | 0 | return(cur); |
3214 | | |
3215 | 0 | return(xmlInsertNode(node->doc, cur, node->parent, node, NULL, 1)); |
3216 | 0 | } |
3217 | | |
3218 | | /** |
3219 | | * xmlAddChildList: |
3220 | | * @parent: the parent node |
3221 | | * @cur: the first node in the list |
3222 | | * |
3223 | | * Append a node list to another node. |
3224 | | * |
3225 | | * See xmlAddChild. |
3226 | | * |
3227 | | * Returns the last child or NULL in case of error. |
3228 | | */ |
3229 | | xmlNodePtr |
3230 | 0 | xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { |
3231 | 0 | xmlNodePtr iter; |
3232 | 0 | xmlNodePtr prev; |
3233 | 0 | int oom; |
3234 | |
|
3235 | 0 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
3236 | 0 | return(NULL); |
3237 | 0 | } |
3238 | | |
3239 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
3240 | 0 | return(NULL); |
3241 | 0 | } |
3242 | | |
3243 | 0 | oom = 0; |
3244 | 0 | for (iter = cur; iter != NULL; iter = iter->next) { |
3245 | 0 | if (iter->doc != parent->doc) { |
3246 | 0 | if (xmlSetTreeDoc(iter, parent->doc) < 0) |
3247 | 0 | oom = 1; |
3248 | 0 | } |
3249 | 0 | } |
3250 | 0 | if (oom) |
3251 | 0 | return(NULL); |
3252 | | |
3253 | | /* |
3254 | | * add the first element at the end of the children list. |
3255 | | */ |
3256 | | |
3257 | 0 | if (parent->children == NULL) { |
3258 | 0 | parent->children = cur; |
3259 | 0 | } else { |
3260 | 0 | prev = parent->last; |
3261 | | |
3262 | | /* |
3263 | | * If cur and parent->last both are TEXT nodes, then merge them. |
3264 | | */ |
3265 | 0 | if ((cur->type == XML_TEXT_NODE) && |
3266 | 0 | (prev->type == XML_TEXT_NODE) && |
3267 | 0 | (cur->name == prev->name)) { |
3268 | 0 | xmlNodePtr next; |
3269 | |
|
3270 | 0 | if (xmlTextAddContent(prev, cur->content, -1) < 0) |
3271 | 0 | return(NULL); |
3272 | 0 | next = cur->next; |
3273 | 0 | xmlFreeNode(cur); |
3274 | | /* |
3275 | | * if it's the only child, nothing more to be done. |
3276 | | */ |
3277 | 0 | if (next == NULL) |
3278 | 0 | return(prev); |
3279 | 0 | cur = next; |
3280 | 0 | } |
3281 | | |
3282 | 0 | prev->next = cur; |
3283 | 0 | cur->prev = prev; |
3284 | 0 | } |
3285 | 0 | while (cur->next != NULL) { |
3286 | 0 | cur->parent = parent; |
3287 | 0 | cur = cur->next; |
3288 | 0 | } |
3289 | 0 | cur->parent = parent; |
3290 | 0 | parent->last = cur; |
3291 | |
|
3292 | 0 | return(cur); |
3293 | 0 | } |
3294 | | |
3295 | | /** |
3296 | | * xmlAddChild: |
3297 | | * @parent: the parent node |
3298 | | * @cur: the child node |
3299 | | * |
3300 | | * Unlink @cur and append it to the children of @parent. |
3301 | | * |
3302 | | * If @cur is a text node, it may be merged with an adjacent text |
3303 | | * node and freed. In this case the text node containing the merged |
3304 | | * content is returned. |
3305 | | * |
3306 | | * If @cur is an attribute node, it is appended to the attributes of |
3307 | | * @parent. If the attribute list contains an attribute with a name |
3308 | | * matching @elem, the old attribute is destroyed. |
3309 | | * |
3310 | | * General notes: |
3311 | | * |
3312 | | * Move operations like xmlAddChild can cause element or attribute |
3313 | | * nodes to reference namespaces that aren't declared in one of |
3314 | | * their ancestors. This can lead to use-after-free errors if the |
3315 | | * elements containing the declarations are freed later, especially |
3316 | | * when moving nodes from one document to another. You should |
3317 | | * consider calling xmlReconciliateNs after a move operation to |
3318 | | * normalize namespaces. Another option is to call |
3319 | | * xmlDOMWrapAdoptNode with the target parent before moving a node. |
3320 | | * |
3321 | | * For the most part, move operations don't check whether the |
3322 | | * resulting tree structure is valid. Users must make sure that |
3323 | | * parent nodes only receive children of valid types. Inserted |
3324 | | * child nodes must never be an ancestor of the parent node to |
3325 | | * avoid cycles in the tree structure. In general, only |
3326 | | * document, document fragments, elements and attributes |
3327 | | * should be used as parent nodes. |
3328 | | * |
3329 | | * When moving a node between documents and a memory allocation |
3330 | | * fails, the node's content will be corrupted and it will be |
3331 | | * unlinked. In this case, the node must be freed manually. |
3332 | | * |
3333 | | * Moving DTDs between documents isn't supported. |
3334 | | * |
3335 | | * Returns @elem or a sibling if @elem was merged. Returns NULL |
3336 | | * if arguments are invalid or a memory allocation failed. |
3337 | | */ |
3338 | | xmlNodePtr |
3339 | 0 | xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { |
3340 | 0 | xmlNodePtr prev; |
3341 | |
|
3342 | 0 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL) || |
3343 | 0 | (cur == NULL) || (cur->type == XML_NAMESPACE_DECL) || |
3344 | 0 | (parent == cur)) |
3345 | 0 | return(NULL); |
3346 | | |
3347 | | /* |
3348 | | * If parent is a text node, call xmlTextAddContent. This |
3349 | | * undocumented quirk should probably be removed. |
3350 | | */ |
3351 | 0 | if (parent->type == XML_TEXT_NODE) { |
3352 | 0 | if (xmlTextAddContent(parent, cur->content, -1) < 0) |
3353 | 0 | return(NULL); |
3354 | 0 | xmlFreeNode(cur); |
3355 | 0 | return(parent); |
3356 | 0 | } |
3357 | | |
3358 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
3359 | 0 | prev = (xmlNodePtr) parent->properties; |
3360 | 0 | if (prev != NULL) { |
3361 | 0 | while (prev->next != NULL) |
3362 | 0 | prev = prev->next; |
3363 | 0 | } |
3364 | 0 | } else { |
3365 | 0 | prev = parent->last; |
3366 | 0 | } |
3367 | |
|
3368 | 0 | if (cur == prev) |
3369 | 0 | return(cur); |
3370 | | |
3371 | 0 | return(xmlInsertNode(parent->doc, cur, parent, prev, NULL, 1)); |
3372 | 0 | } |
3373 | | |
3374 | | /** |
3375 | | * xmlGetLastChild: |
3376 | | * @parent: the parent node |
3377 | | * |
3378 | | * Find the last child of a node. |
3379 | | * |
3380 | | * Returns the last child or NULL if parent has no children. |
3381 | | */ |
3382 | | xmlNodePtr |
3383 | 0 | xmlGetLastChild(const xmlNode *parent) { |
3384 | 0 | if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { |
3385 | 0 | return(NULL); |
3386 | 0 | } |
3387 | 0 | return(parent->last); |
3388 | 0 | } |
3389 | | |
3390 | | /* |
3391 | | * 5 interfaces from DOM ElementTraversal |
3392 | | */ |
3393 | | |
3394 | | /** |
3395 | | * xmlChildElementCount: |
3396 | | * @parent: the parent node |
3397 | | * |
3398 | | * Count the number of child nodes which are elements. |
3399 | | * |
3400 | | * Note that entity references are not expanded. |
3401 | | * |
3402 | | * Returns the number of element children or 0 if arguments are |
3403 | | * invalid. |
3404 | | */ |
3405 | | unsigned long |
3406 | 10 | xmlChildElementCount(xmlNodePtr parent) { |
3407 | 10 | unsigned long ret = 0; |
3408 | 10 | xmlNodePtr cur = NULL; |
3409 | | |
3410 | 10 | if (parent == NULL) |
3411 | 0 | return(0); |
3412 | 10 | switch (parent->type) { |
3413 | 10 | case XML_ELEMENT_NODE: |
3414 | 10 | case XML_DOCUMENT_NODE: |
3415 | 10 | case XML_DOCUMENT_FRAG_NODE: |
3416 | 10 | case XML_HTML_DOCUMENT_NODE: |
3417 | 10 | case XML_ENTITY_DECL: |
3418 | 10 | cur = parent->children; |
3419 | 10 | break; |
3420 | 0 | default: |
3421 | 0 | return(0); |
3422 | 10 | } |
3423 | 110 | while (cur != NULL) { |
3424 | 100 | if (cur->type == XML_ELEMENT_NODE) |
3425 | 45 | ret++; |
3426 | 100 | cur = cur->next; |
3427 | 100 | } |
3428 | 10 | return(ret); |
3429 | 10 | } |
3430 | | |
3431 | | /** |
3432 | | * xmlFirstElementChild: |
3433 | | * @parent: the parent node |
3434 | | * |
3435 | | * Find the first child node which is an element. |
3436 | | * |
3437 | | * Note that entity references are not expanded. |
3438 | | * |
3439 | | * Returns the first element or NULL if parent has no children. |
3440 | | */ |
3441 | | xmlNodePtr |
3442 | 474 | xmlFirstElementChild(xmlNodePtr parent) { |
3443 | 474 | xmlNodePtr cur = NULL; |
3444 | | |
3445 | 474 | if (parent == NULL) |
3446 | 0 | return(NULL); |
3447 | 474 | switch (parent->type) { |
3448 | 474 | case XML_ELEMENT_NODE: |
3449 | 474 | case XML_DOCUMENT_NODE: |
3450 | 474 | case XML_DOCUMENT_FRAG_NODE: |
3451 | 474 | case XML_HTML_DOCUMENT_NODE: |
3452 | 474 | case XML_ENTITY_DECL: |
3453 | 474 | cur = parent->children; |
3454 | 474 | break; |
3455 | 0 | default: |
3456 | 0 | return(NULL); |
3457 | 474 | } |
3458 | 948 | while (cur != NULL) { |
3459 | 838 | if (cur->type == XML_ELEMENT_NODE) |
3460 | 364 | return(cur); |
3461 | 474 | cur = cur->next; |
3462 | 474 | } |
3463 | 110 | return(NULL); |
3464 | 474 | } |
3465 | | |
3466 | | /** |
3467 | | * xmlLastElementChild: |
3468 | | * @parent: the parent node |
3469 | | * |
3470 | | * Find the last child node which is an element. |
3471 | | * |
3472 | | * Note that entity references are not expanded. |
3473 | | * |
3474 | | * Returns the last element or NULL if parent has no children. |
3475 | | */ |
3476 | | xmlNodePtr |
3477 | 0 | xmlLastElementChild(xmlNodePtr parent) { |
3478 | 0 | xmlNodePtr cur = NULL; |
3479 | |
|
3480 | 0 | if (parent == NULL) |
3481 | 0 | return(NULL); |
3482 | 0 | switch (parent->type) { |
3483 | 0 | case XML_ELEMENT_NODE: |
3484 | 0 | case XML_DOCUMENT_NODE: |
3485 | 0 | case XML_DOCUMENT_FRAG_NODE: |
3486 | 0 | case XML_HTML_DOCUMENT_NODE: |
3487 | 0 | case XML_ENTITY_DECL: |
3488 | 0 | cur = parent->last; |
3489 | 0 | break; |
3490 | 0 | default: |
3491 | 0 | return(NULL); |
3492 | 0 | } |
3493 | 0 | while (cur != NULL) { |
3494 | 0 | if (cur->type == XML_ELEMENT_NODE) |
3495 | 0 | return(cur); |
3496 | 0 | cur = cur->prev; |
3497 | 0 | } |
3498 | 0 | return(NULL); |
3499 | 0 | } |
3500 | | |
3501 | | /** |
3502 | | * xmlPreviousElementSibling: |
3503 | | * @node: the current node |
3504 | | * |
3505 | | * Find the closest preceding sibling which is a element. |
3506 | | * |
3507 | | * Note that entity references are not expanded. |
3508 | | * |
3509 | | * Returns the sibling or NULL if no sibling was found. |
3510 | | */ |
3511 | | xmlNodePtr |
3512 | 0 | xmlPreviousElementSibling(xmlNodePtr node) { |
3513 | 0 | if (node == NULL) |
3514 | 0 | return(NULL); |
3515 | 0 | switch (node->type) { |
3516 | 0 | case XML_ELEMENT_NODE: |
3517 | 0 | case XML_TEXT_NODE: |
3518 | 0 | case XML_CDATA_SECTION_NODE: |
3519 | 0 | case XML_ENTITY_REF_NODE: |
3520 | 0 | case XML_PI_NODE: |
3521 | 0 | case XML_COMMENT_NODE: |
3522 | 0 | case XML_XINCLUDE_START: |
3523 | 0 | case XML_XINCLUDE_END: |
3524 | 0 | node = node->prev; |
3525 | 0 | break; |
3526 | 0 | default: |
3527 | 0 | return(NULL); |
3528 | 0 | } |
3529 | 0 | while (node != NULL) { |
3530 | 0 | if (node->type == XML_ELEMENT_NODE) |
3531 | 0 | return(node); |
3532 | 0 | node = node->prev; |
3533 | 0 | } |
3534 | 0 | return(NULL); |
3535 | 0 | } |
3536 | | |
3537 | | /** |
3538 | | * xmlNextElementSibling: |
3539 | | * @node: the current node |
3540 | | * |
3541 | | * Find the closest following sibling which is a element. |
3542 | | * |
3543 | | * Note that entity references are not expanded. |
3544 | | * |
3545 | | * Returns the sibling or NULL if no sibling was found. |
3546 | | */ |
3547 | | xmlNodePtr |
3548 | 1.38k | xmlNextElementSibling(xmlNodePtr node) { |
3549 | 1.38k | if (node == NULL) |
3550 | 0 | return(NULL); |
3551 | 1.38k | switch (node->type) { |
3552 | 1.38k | case XML_ELEMENT_NODE: |
3553 | 1.38k | case XML_TEXT_NODE: |
3554 | 1.38k | case XML_CDATA_SECTION_NODE: |
3555 | 1.38k | case XML_ENTITY_REF_NODE: |
3556 | 1.38k | case XML_PI_NODE: |
3557 | 1.38k | case XML_COMMENT_NODE: |
3558 | 1.38k | case XML_DTD_NODE: |
3559 | 1.38k | case XML_XINCLUDE_START: |
3560 | 1.38k | case XML_XINCLUDE_END: |
3561 | 1.38k | node = node->next; |
3562 | 1.38k | break; |
3563 | 0 | default: |
3564 | 0 | return(NULL); |
3565 | 1.38k | } |
3566 | 2.76k | while (node != NULL) { |
3567 | 2.61k | if (node->type == XML_ELEMENT_NODE) |
3568 | 1.23k | return(node); |
3569 | 1.38k | node = node->next; |
3570 | 1.38k | } |
3571 | 143 | return(NULL); |
3572 | 1.38k | } |
3573 | | |
3574 | | /** |
3575 | | * xmlFreeNodeList: |
3576 | | * @cur: the first node in the list |
3577 | | * |
3578 | | * Free a node list including all children. |
3579 | | */ |
3580 | | void |
3581 | 31.6k | xmlFreeNodeList(xmlNodePtr cur) { |
3582 | 31.6k | xmlNodePtr next; |
3583 | 31.6k | xmlNodePtr parent; |
3584 | 31.6k | xmlDictPtr dict = NULL; |
3585 | 31.6k | size_t depth = 0; |
3586 | | |
3587 | 31.6k | if (cur == NULL) return; |
3588 | 31.2k | if (cur->type == XML_NAMESPACE_DECL) { |
3589 | 0 | xmlFreeNsList((xmlNsPtr) cur); |
3590 | 0 | return; |
3591 | 0 | } |
3592 | 31.2k | if (cur->doc != NULL) dict = cur->doc->dict; |
3593 | 102k | while (1) { |
3594 | 136k | while ((cur->children != NULL) && |
3595 | 136k | (cur->type != XML_DOCUMENT_NODE) && |
3596 | 136k | (cur->type != XML_HTML_DOCUMENT_NODE) && |
3597 | 136k | (cur->type != XML_DTD_NODE) && |
3598 | 136k | (cur->type != XML_ENTITY_REF_NODE)) { |
3599 | 33.4k | cur = cur->children; |
3600 | 33.4k | depth += 1; |
3601 | 33.4k | } |
3602 | | |
3603 | 102k | next = cur->next; |
3604 | 102k | parent = cur->parent; |
3605 | 102k | if ((cur->type == XML_DOCUMENT_NODE) || |
3606 | 102k | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
3607 | 0 | xmlFreeDoc((xmlDocPtr) cur); |
3608 | 102k | } else if (cur->type == XML_DTD_NODE) { |
3609 | | /* |
3610 | | * TODO: We should consider freeing the DTD if it isn't |
3611 | | * referenced from doc->intSubset or doc->extSubset. |
3612 | | */ |
3613 | 0 | cur->prev = NULL; |
3614 | 0 | cur->next = NULL; |
3615 | 102k | } else { |
3616 | 102k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
3617 | 0 | xmlDeregisterNodeDefaultValue(cur); |
3618 | | |
3619 | 102k | if (((cur->type == XML_ELEMENT_NODE) || |
3620 | 102k | (cur->type == XML_XINCLUDE_START) || |
3621 | 102k | (cur->type == XML_XINCLUDE_END)) && |
3622 | 102k | (cur->properties != NULL)) |
3623 | 16.8k | xmlFreePropList(cur->properties); |
3624 | 102k | if ((cur->type != XML_ELEMENT_NODE) && |
3625 | 102k | (cur->type != XML_XINCLUDE_START) && |
3626 | 102k | (cur->type != XML_XINCLUDE_END) && |
3627 | 102k | (cur->type != XML_ENTITY_REF_NODE) && |
3628 | 102k | (cur->content != (xmlChar *) &(cur->properties))) { |
3629 | 53.0k | DICT_FREE(cur->content) |
3630 | 53.0k | } |
3631 | 102k | if (((cur->type == XML_ELEMENT_NODE) || |
3632 | 102k | (cur->type == XML_XINCLUDE_START) || |
3633 | 102k | (cur->type == XML_XINCLUDE_END)) && |
3634 | 102k | (cur->nsDef != NULL)) |
3635 | 4.08k | xmlFreeNsList(cur->nsDef); |
3636 | | |
3637 | | /* |
3638 | | * When a node is a text node or a comment, it uses a global static |
3639 | | * variable for the name of the node. |
3640 | | * Otherwise the node name might come from the document's |
3641 | | * dictionary |
3642 | | */ |
3643 | 102k | if ((cur->name != NULL) && |
3644 | 102k | (cur->type != XML_TEXT_NODE) && |
3645 | 102k | (cur->type != XML_COMMENT_NODE)) |
3646 | 53.8k | DICT_FREE(cur->name) |
3647 | 102k | xmlFree(cur); |
3648 | 102k | } |
3649 | | |
3650 | 102k | if (next != NULL) { |
3651 | 38.1k | cur = next; |
3652 | 64.7k | } else { |
3653 | 64.7k | if ((depth == 0) || (parent == NULL)) |
3654 | 31.2k | break; |
3655 | 33.4k | depth -= 1; |
3656 | 33.4k | cur = parent; |
3657 | 33.4k | cur->children = NULL; |
3658 | 33.4k | } |
3659 | 102k | } |
3660 | 31.2k | } |
3661 | | |
3662 | | /** |
3663 | | * xmlFreeNode: |
3664 | | * @cur: the node |
3665 | | * |
3666 | | * Free a node including all the children. |
3667 | | * |
3668 | | * This doesn't unlink the node from the tree. Call xmlUnlinkNode first |
3669 | | * unless @cur is a root node. |
3670 | | */ |
3671 | | void |
3672 | 13.1k | xmlFreeNode(xmlNodePtr cur) { |
3673 | 13.1k | xmlDictPtr dict = NULL; |
3674 | | |
3675 | 13.1k | if (cur == NULL) return; |
3676 | | |
3677 | | /* use xmlFreeDtd for DTD nodes */ |
3678 | 3.21k | if (cur->type == XML_DTD_NODE) { |
3679 | 0 | xmlFreeDtd((xmlDtdPtr) cur); |
3680 | 0 | return; |
3681 | 0 | } |
3682 | 3.21k | if (cur->type == XML_NAMESPACE_DECL) { |
3683 | 0 | xmlFreeNs((xmlNsPtr) cur); |
3684 | 0 | return; |
3685 | 0 | } |
3686 | 3.21k | if (cur->type == XML_ATTRIBUTE_NODE) { |
3687 | 0 | xmlFreeProp((xmlAttrPtr) cur); |
3688 | 0 | return; |
3689 | 0 | } |
3690 | 3.21k | if (cur->type == XML_ENTITY_DECL) { |
3691 | 0 | xmlFreeEntity((xmlEntityPtr) cur); |
3692 | 0 | return; |
3693 | 0 | } |
3694 | | |
3695 | 3.21k | if ((xmlRegisterCallbacks) && (xmlDeregisterNodeDefaultValue)) |
3696 | 0 | xmlDeregisterNodeDefaultValue(cur); |
3697 | | |
3698 | 3.21k | if (cur->doc != NULL) dict = cur->doc->dict; |
3699 | | |
3700 | 3.21k | if ((cur->children != NULL) && |
3701 | 3.21k | (cur->type != XML_ENTITY_REF_NODE)) |
3702 | 199 | xmlFreeNodeList(cur->children); |
3703 | | |
3704 | 3.21k | if ((cur->type == XML_ELEMENT_NODE) || |
3705 | 3.21k | (cur->type == XML_XINCLUDE_START) || |
3706 | 3.21k | (cur->type == XML_XINCLUDE_END)) { |
3707 | 863 | if (cur->properties != NULL) |
3708 | 0 | xmlFreePropList(cur->properties); |
3709 | 863 | if (cur->nsDef != NULL) |
3710 | 0 | xmlFreeNsList(cur->nsDef); |
3711 | 2.34k | } else if ((cur->content != NULL) && |
3712 | 2.34k | (cur->type != XML_ENTITY_REF_NODE) && |
3713 | 2.34k | (cur->content != (xmlChar *) &(cur->properties))) { |
3714 | 942 | DICT_FREE(cur->content) |
3715 | 942 | } |
3716 | | |
3717 | | /* |
3718 | | * When a node is a text node or a comment, it uses a global static |
3719 | | * variable for the name of the node. |
3720 | | * Otherwise the node name might come from the document's dictionary |
3721 | | */ |
3722 | 3.21k | if ((cur->name != NULL) && |
3723 | 3.21k | (cur->type != XML_TEXT_NODE) && |
3724 | 3.21k | (cur->type != XML_COMMENT_NODE)) |
3725 | 2.48k | DICT_FREE(cur->name) |
3726 | | |
3727 | 3.21k | xmlFree(cur); |
3728 | 3.21k | } |
3729 | | |
3730 | | /** |
3731 | | * xmlUnlinkNodeInternal: |
3732 | | * @cur: the node |
3733 | | * |
3734 | | * Unlink a node from its tree. |
3735 | | * |
3736 | | * This function only unlinks the node from the tree. It doesn't |
3737 | | * clear references to DTD nodes. |
3738 | | */ |
3739 | | static void |
3740 | 50.5k | xmlUnlinkNodeInternal(xmlNodePtr cur) { |
3741 | 50.5k | if (cur->parent != NULL) { |
3742 | 28.1k | xmlNodePtr parent; |
3743 | 28.1k | parent = cur->parent; |
3744 | 28.1k | if (cur->type == XML_ATTRIBUTE_NODE) { |
3745 | 0 | if (parent->properties == (xmlAttrPtr) cur) |
3746 | 0 | parent->properties = ((xmlAttrPtr) cur)->next; |
3747 | 28.1k | } else { |
3748 | 28.1k | if (parent->children == cur) |
3749 | 9.74k | parent->children = cur->next; |
3750 | 28.1k | if (parent->last == cur) |
3751 | 7.81k | parent->last = cur->prev; |
3752 | 28.1k | } |
3753 | 28.1k | cur->parent = NULL; |
3754 | 28.1k | } |
3755 | | |
3756 | 50.5k | if (cur->next != NULL) |
3757 | 20.3k | cur->next->prev = cur->prev; |
3758 | 50.5k | if (cur->prev != NULL) |
3759 | 18.4k | cur->prev->next = cur->next; |
3760 | 50.5k | cur->next = NULL; |
3761 | 50.5k | cur->prev = NULL; |
3762 | 50.5k | } |
3763 | | |
3764 | | /** |
3765 | | * xmlUnlinkNode: |
3766 | | * @cur: the node |
3767 | | * |
3768 | | * Unlink a node from its tree. |
3769 | | * |
3770 | | * The node is not freed. Unless it is reinserted, it must be managed |
3771 | | * manually and freed eventually by calling xmlFreeNode. |
3772 | | */ |
3773 | | void |
3774 | 41.9k | xmlUnlinkNode(xmlNodePtr cur) { |
3775 | 41.9k | if (cur == NULL) |
3776 | 0 | return; |
3777 | | |
3778 | 41.9k | if (cur->type == XML_NAMESPACE_DECL) |
3779 | 0 | return; |
3780 | | |
3781 | 41.9k | if (cur->type == XML_DTD_NODE) { |
3782 | 0 | xmlDocPtr doc = cur->doc; |
3783 | |
|
3784 | 0 | if (doc != NULL) { |
3785 | 0 | if (doc->intSubset == (xmlDtdPtr) cur) |
3786 | 0 | doc->intSubset = NULL; |
3787 | 0 | if (doc->extSubset == (xmlDtdPtr) cur) |
3788 | 0 | doc->extSubset = NULL; |
3789 | 0 | } |
3790 | 0 | } |
3791 | | |
3792 | 41.9k | if (cur->type == XML_ENTITY_DECL) |
3793 | 0 | xmlRemoveEntity((xmlEntityPtr) cur); |
3794 | | |
3795 | 41.9k | xmlUnlinkNodeInternal(cur); |
3796 | 41.9k | } |
3797 | | |
3798 | | /** |
3799 | | * xmlReplaceNode: |
3800 | | * @old: the old node |
3801 | | * @cur: the node (optional) |
3802 | | * |
3803 | | * Unlink the old node. If @cur is provided, it is unlinked and |
3804 | | * inserted in place of @old. |
3805 | | * |
3806 | | * It is an error if @old has no parent. |
3807 | | * |
3808 | | * Unlike xmlAddChild, this function doesn't merge text nodes or |
3809 | | * delete duplicate attributes. |
3810 | | * |
3811 | | * See the notes in xmlAddChild. |
3812 | | * |
3813 | | * Returns @old or NULL if arguments are invalid or a memory |
3814 | | * allocation failed. |
3815 | | */ |
3816 | | xmlNodePtr |
3817 | 0 | xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { |
3818 | 0 | if (old == cur) return(NULL); |
3819 | 0 | if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) || |
3820 | 0 | (old->parent == NULL)) { |
3821 | 0 | return(NULL); |
3822 | 0 | } |
3823 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { |
3824 | | /* Don't call xmlUnlinkNodeInternal to handle DTDs. */ |
3825 | 0 | xmlUnlinkNode(old); |
3826 | 0 | return(old); |
3827 | 0 | } |
3828 | 0 | if ((old->type==XML_ATTRIBUTE_NODE) && (cur->type!=XML_ATTRIBUTE_NODE)) { |
3829 | 0 | return(old); |
3830 | 0 | } |
3831 | 0 | if ((cur->type==XML_ATTRIBUTE_NODE) && (old->type!=XML_ATTRIBUTE_NODE)) { |
3832 | 0 | return(old); |
3833 | 0 | } |
3834 | 0 | xmlUnlinkNodeInternal(cur); |
3835 | 0 | if (xmlSetTreeDoc(cur, old->doc) < 0) |
3836 | 0 | return(NULL); |
3837 | 0 | cur->parent = old->parent; |
3838 | 0 | cur->next = old->next; |
3839 | 0 | if (cur->next != NULL) |
3840 | 0 | cur->next->prev = cur; |
3841 | 0 | cur->prev = old->prev; |
3842 | 0 | if (cur->prev != NULL) |
3843 | 0 | cur->prev->next = cur; |
3844 | 0 | if (cur->parent != NULL) { |
3845 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
3846 | 0 | if (cur->parent->properties == (xmlAttrPtr)old) |
3847 | 0 | cur->parent->properties = ((xmlAttrPtr) cur); |
3848 | 0 | } else { |
3849 | 0 | if (cur->parent->children == old) |
3850 | 0 | cur->parent->children = cur; |
3851 | 0 | if (cur->parent->last == old) |
3852 | 0 | cur->parent->last = cur; |
3853 | 0 | } |
3854 | 0 | } |
3855 | 0 | old->next = old->prev = NULL; |
3856 | 0 | old->parent = NULL; |
3857 | 0 | return(old); |
3858 | 0 | } |
3859 | | |
3860 | | /************************************************************************ |
3861 | | * * |
3862 | | * Copy operations * |
3863 | | * * |
3864 | | ************************************************************************/ |
3865 | | |
3866 | | /** |
3867 | | * xmlCopyNamespace: |
3868 | | * @cur: the namespace |
3869 | | * |
3870 | | * Copy a namespace. |
3871 | | * |
3872 | | * Returns the copied namespace or NULL if a memory allocation |
3873 | | * failed. |
3874 | | */ |
3875 | | xmlNsPtr |
3876 | 0 | xmlCopyNamespace(xmlNsPtr cur) { |
3877 | 0 | xmlNsPtr ret; |
3878 | |
|
3879 | 0 | if (cur == NULL) return(NULL); |
3880 | 0 | switch (cur->type) { |
3881 | 0 | case XML_LOCAL_NAMESPACE: |
3882 | 0 | ret = xmlNewNs(NULL, cur->href, cur->prefix); |
3883 | 0 | break; |
3884 | 0 | default: |
3885 | 0 | return(NULL); |
3886 | 0 | } |
3887 | 0 | return(ret); |
3888 | 0 | } |
3889 | | |
3890 | | /** |
3891 | | * xmlCopyNamespaceList: |
3892 | | * @cur: the first namespace |
3893 | | * |
3894 | | * Copy a namespace list. |
3895 | | * |
3896 | | * Returns the head of the copied list or NULL if a memory |
3897 | | * allocation failed. |
3898 | | */ |
3899 | | xmlNsPtr |
3900 | 0 | xmlCopyNamespaceList(xmlNsPtr cur) { |
3901 | 0 | xmlNsPtr ret = NULL; |
3902 | 0 | xmlNsPtr p = NULL,q; |
3903 | |
|
3904 | 0 | while (cur != NULL) { |
3905 | 0 | q = xmlCopyNamespace(cur); |
3906 | 0 | if (q == NULL) { |
3907 | 0 | xmlFreeNsList(ret); |
3908 | 0 | return(NULL); |
3909 | 0 | } |
3910 | 0 | if (p == NULL) { |
3911 | 0 | ret = p = q; |
3912 | 0 | } else { |
3913 | 0 | p->next = q; |
3914 | 0 | p = q; |
3915 | 0 | } |
3916 | 0 | cur = cur->next; |
3917 | 0 | } |
3918 | 0 | return(ret); |
3919 | 0 | } |
3920 | | |
3921 | | static xmlAttrPtr |
3922 | 0 | xmlCopyPropInternal(xmlDocPtr doc, xmlNodePtr target, xmlAttrPtr cur) { |
3923 | 0 | xmlAttrPtr ret = NULL; |
3924 | |
|
3925 | 0 | if (cur == NULL) return(NULL); |
3926 | 0 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
3927 | 0 | return(NULL); |
3928 | 0 | if (target != NULL) |
3929 | 0 | ret = xmlNewDocProp(target->doc, cur->name, NULL); |
3930 | 0 | else if (doc != NULL) |
3931 | 0 | ret = xmlNewDocProp(doc, cur->name, NULL); |
3932 | 0 | else if (cur->parent != NULL) |
3933 | 0 | ret = xmlNewDocProp(cur->parent->doc, cur->name, NULL); |
3934 | 0 | else if (cur->children != NULL) |
3935 | 0 | ret = xmlNewDocProp(cur->children->doc, cur->name, NULL); |
3936 | 0 | else |
3937 | 0 | ret = xmlNewDocProp(NULL, cur->name, NULL); |
3938 | 0 | if (ret == NULL) return(NULL); |
3939 | 0 | ret->parent = target; |
3940 | |
|
3941 | 0 | if ((cur->ns != NULL) && (target != NULL)) { |
3942 | 0 | xmlNsPtr ns; |
3943 | 0 | int res; |
3944 | |
|
3945 | 0 | res = xmlSearchNsSafe(target, cur->ns->prefix, &ns); |
3946 | 0 | if (res < 0) |
3947 | 0 | goto error; |
3948 | 0 | if (ns == NULL) { |
3949 | | /* |
3950 | | * Humm, we are copying an element whose namespace is defined |
3951 | | * out of the new tree scope. Search it in the original tree |
3952 | | * and add it at the top of the new tree |
3953 | | */ |
3954 | 0 | res = xmlSearchNsSafe(cur->parent, cur->ns->prefix, &ns); |
3955 | 0 | if (res < 0) |
3956 | 0 | goto error; |
3957 | 0 | if (ns != NULL) { |
3958 | 0 | xmlNodePtr root = target; |
3959 | 0 | xmlNodePtr pred = NULL; |
3960 | |
|
3961 | 0 | while (root->parent != NULL) { |
3962 | 0 | pred = root; |
3963 | 0 | root = root->parent; |
3964 | 0 | } |
3965 | 0 | if (root == (xmlNodePtr) target->doc) { |
3966 | | /* correct possibly cycling above the document elt */ |
3967 | 0 | root = pred; |
3968 | 0 | } |
3969 | 0 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
3970 | 0 | if (ret->ns == NULL) |
3971 | 0 | goto error; |
3972 | 0 | } |
3973 | 0 | } else { |
3974 | | /* |
3975 | | * we have to find something appropriate here since |
3976 | | * we can't be sure, that the namespace we found is identified |
3977 | | * by the prefix |
3978 | | */ |
3979 | 0 | if (xmlStrEqual(ns->href, cur->ns->href)) { |
3980 | | /* this is the nice case */ |
3981 | 0 | ret->ns = ns; |
3982 | 0 | } else { |
3983 | | /* |
3984 | | * we are in trouble: we need a new reconciled namespace. |
3985 | | * This is expensive |
3986 | | */ |
3987 | 0 | ret->ns = xmlNewReconciledNs(target, cur->ns); |
3988 | 0 | if (ret->ns == NULL) |
3989 | 0 | goto error; |
3990 | 0 | } |
3991 | 0 | } |
3992 | |
|
3993 | 0 | } else |
3994 | 0 | ret->ns = NULL; |
3995 | | |
3996 | 0 | if (cur->children != NULL) { |
3997 | 0 | xmlNodePtr tmp; |
3998 | |
|
3999 | 0 | ret->children = xmlStaticCopyNodeList(cur->children, ret->doc, (xmlNodePtr) ret); |
4000 | 0 | if (ret->children == NULL) |
4001 | 0 | goto error; |
4002 | 0 | ret->last = NULL; |
4003 | 0 | tmp = ret->children; |
4004 | 0 | while (tmp != NULL) { |
4005 | | /* tmp->parent = (xmlNodePtr)ret; */ |
4006 | 0 | if (tmp->next == NULL) |
4007 | 0 | ret->last = tmp; |
4008 | 0 | tmp = tmp->next; |
4009 | 0 | } |
4010 | 0 | } |
4011 | | /* |
4012 | | * Try to handle IDs |
4013 | | */ |
4014 | 0 | if ((target != NULL) && (cur != NULL) && |
4015 | 0 | (target->doc != NULL) && (cur->doc != NULL) && |
4016 | 0 | (cur->parent != NULL) && |
4017 | 0 | (cur->children != NULL)) { |
4018 | 0 | int res = xmlIsID(cur->doc, cur->parent, cur); |
4019 | |
|
4020 | 0 | if (res < 0) |
4021 | 0 | goto error; |
4022 | 0 | if (res != 0) { |
4023 | 0 | xmlChar *id; |
4024 | |
|
4025 | 0 | id = xmlNodeGetContent((xmlNodePtr) cur); |
4026 | 0 | if (id == NULL) |
4027 | 0 | goto error; |
4028 | 0 | res = xmlAddIDSafe(ret, id); |
4029 | 0 | xmlFree(id); |
4030 | 0 | if (res < 0) |
4031 | 0 | goto error; |
4032 | 0 | } |
4033 | 0 | } |
4034 | 0 | return(ret); |
4035 | | |
4036 | 0 | error: |
4037 | 0 | xmlFreeProp(ret); |
4038 | 0 | return(NULL); |
4039 | 0 | } |
4040 | | |
4041 | | /** |
4042 | | * xmlCopyProp: |
4043 | | * @target: the element where the attribute will be grafted |
4044 | | * @cur: the attribute |
4045 | | * |
4046 | | * Create a copy of the attribute. This function sets the parent |
4047 | | * pointer of the copy to @target but doesn't set the attribute on |
4048 | | * the target element. Users should consider to set the attribute |
4049 | | * by calling xmlAddChild afterwards or reset the parent pointer to |
4050 | | * NULL. |
4051 | | * |
4052 | | * Returns the copied attribute or NULL if a memory allocation |
4053 | | * failed. |
4054 | | */ |
4055 | | xmlAttrPtr |
4056 | 0 | xmlCopyProp(xmlNodePtr target, xmlAttrPtr cur) { |
4057 | 0 | return xmlCopyPropInternal(NULL, target, cur); |
4058 | 0 | } |
4059 | | |
4060 | | /** |
4061 | | * xmlCopyPropList: |
4062 | | * @target: the element where the attributes will be grafted |
4063 | | * @cur: the first attribute |
4064 | | * |
4065 | | * Create a copy of an attribute list. This function sets the |
4066 | | * parent pointers of the copied attributes to @target but doesn't |
4067 | | * set the attributes on the target element. |
4068 | | * |
4069 | | * Returns the head of the copied list or NULL if a memory |
4070 | | * allocation failed. |
4071 | | */ |
4072 | | xmlAttrPtr |
4073 | 0 | xmlCopyPropList(xmlNodePtr target, xmlAttrPtr cur) { |
4074 | 0 | xmlAttrPtr ret = NULL; |
4075 | 0 | xmlAttrPtr p = NULL,q; |
4076 | |
|
4077 | 0 | if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) |
4078 | 0 | return(NULL); |
4079 | 0 | while (cur != NULL) { |
4080 | 0 | q = xmlCopyProp(target, cur); |
4081 | 0 | if (q == NULL) { |
4082 | 0 | xmlFreePropList(ret); |
4083 | 0 | return(NULL); |
4084 | 0 | } |
4085 | 0 | if (p == NULL) { |
4086 | 0 | ret = p = q; |
4087 | 0 | } else { |
4088 | 0 | p->next = q; |
4089 | 0 | q->prev = p; |
4090 | 0 | p = q; |
4091 | 0 | } |
4092 | 0 | cur = cur->next; |
4093 | 0 | } |
4094 | 0 | return(ret); |
4095 | 0 | } |
4096 | | |
4097 | | /* |
4098 | | * NOTE about the CopyNode operations ! |
4099 | | * |
4100 | | * They are split into external and internal parts for one |
4101 | | * tricky reason: namespaces. Doing a direct copy of a node |
4102 | | * say RPM:Copyright without changing the namespace pointer to |
4103 | | * something else can produce stale links. One way to do it is |
4104 | | * to keep a reference counter but this doesn't work as soon |
4105 | | * as one moves the element or the subtree out of the scope of |
4106 | | * the existing namespace. The actual solution seems to be to add |
4107 | | * a copy of the namespace at the top of the copied tree if |
4108 | | * not available in the subtree. |
4109 | | * Hence two functions, the public front-end call the inner ones |
4110 | | * The argument "recursive" normally indicates a recursive copy |
4111 | | * of the node with values 0 (no) and 1 (yes). For XInclude, |
4112 | | * however, we allow a value of 2 to indicate copy properties and |
4113 | | * namespace info, but don't recurse on children. |
4114 | | */ |
4115 | | |
4116 | | /** |
4117 | | * xmlStaticCopyNode: |
4118 | | * @node: source node |
4119 | | * @doc: target document |
4120 | | * @parent: target parent |
4121 | | * @extended: flags |
4122 | | * |
4123 | | * Copy a node. |
4124 | | * |
4125 | | * Returns the copy or NULL if a memory allocation failed. |
4126 | | */ |
4127 | | xmlNodePtr |
4128 | | xmlStaticCopyNode(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent, |
4129 | 0 | int extended) { |
4130 | 0 | xmlNodePtr ret; |
4131 | |
|
4132 | 0 | if (node == NULL) return(NULL); |
4133 | 0 | switch (node->type) { |
4134 | 0 | case XML_TEXT_NODE: |
4135 | 0 | case XML_CDATA_SECTION_NODE: |
4136 | 0 | case XML_ELEMENT_NODE: |
4137 | 0 | case XML_DOCUMENT_FRAG_NODE: |
4138 | 0 | case XML_ENTITY_REF_NODE: |
4139 | 0 | case XML_PI_NODE: |
4140 | 0 | case XML_COMMENT_NODE: |
4141 | 0 | case XML_XINCLUDE_START: |
4142 | 0 | case XML_XINCLUDE_END: |
4143 | 0 | break; |
4144 | 0 | case XML_ATTRIBUTE_NODE: |
4145 | 0 | return((xmlNodePtr) xmlCopyPropInternal(doc, parent, (xmlAttrPtr) node)); |
4146 | 0 | case XML_NAMESPACE_DECL: |
4147 | 0 | return((xmlNodePtr) xmlCopyNamespaceList((xmlNsPtr) node)); |
4148 | | |
4149 | 0 | case XML_DOCUMENT_NODE: |
4150 | 0 | case XML_HTML_DOCUMENT_NODE: |
4151 | 0 | return((xmlNodePtr) xmlCopyDoc((xmlDocPtr) node, extended)); |
4152 | 0 | default: |
4153 | 0 | return(NULL); |
4154 | 0 | } |
4155 | | |
4156 | | /* |
4157 | | * Allocate a new node and fill the fields. |
4158 | | */ |
4159 | 0 | ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
4160 | 0 | if (ret == NULL) |
4161 | 0 | return(NULL); |
4162 | 0 | memset(ret, 0, sizeof(xmlNode)); |
4163 | 0 | ret->type = node->type; |
4164 | |
|
4165 | 0 | ret->doc = doc; |
4166 | 0 | ret->parent = parent; |
4167 | 0 | if (node->name == xmlStringText) |
4168 | 0 | ret->name = xmlStringText; |
4169 | 0 | else if (node->name == xmlStringTextNoenc) |
4170 | 0 | ret->name = xmlStringTextNoenc; |
4171 | 0 | else if (node->name == xmlStringComment) |
4172 | 0 | ret->name = xmlStringComment; |
4173 | 0 | else if (node->name != NULL) { |
4174 | 0 | if ((doc != NULL) && (doc->dict != NULL)) |
4175 | 0 | ret->name = xmlDictLookup(doc->dict, node->name, -1); |
4176 | 0 | else |
4177 | 0 | ret->name = xmlStrdup(node->name); |
4178 | 0 | if (ret->name == NULL) |
4179 | 0 | goto error; |
4180 | 0 | } |
4181 | 0 | if ((node->type != XML_ELEMENT_NODE) && |
4182 | 0 | (node->content != NULL) && |
4183 | 0 | (node->type != XML_ENTITY_REF_NODE) && |
4184 | 0 | (node->type != XML_XINCLUDE_END) && |
4185 | 0 | (node->type != XML_XINCLUDE_START)) { |
4186 | 0 | ret->content = xmlStrdup(node->content); |
4187 | 0 | if (ret->content == NULL) |
4188 | 0 | goto error; |
4189 | 0 | }else{ |
4190 | 0 | if (node->type == XML_ELEMENT_NODE) |
4191 | 0 | ret->line = node->line; |
4192 | 0 | } |
4193 | | |
4194 | 0 | if (!extended) |
4195 | 0 | goto out; |
4196 | 0 | if (((node->type == XML_ELEMENT_NODE) || |
4197 | 0 | (node->type == XML_XINCLUDE_START)) && (node->nsDef != NULL)) { |
4198 | 0 | ret->nsDef = xmlCopyNamespaceList(node->nsDef); |
4199 | 0 | if (ret->nsDef == NULL) |
4200 | 0 | goto error; |
4201 | 0 | } |
4202 | | |
4203 | 0 | if ((node->type == XML_ELEMENT_NODE) && (node->ns != NULL)) { |
4204 | 0 | xmlNsPtr ns = NULL; |
4205 | 0 | int res; |
4206 | |
|
4207 | 0 | res = xmlSearchNsSafe(ret, node->ns->prefix, &ns); |
4208 | 0 | if (res < 0) |
4209 | 0 | goto error; |
4210 | 0 | if (ns == NULL) { |
4211 | | /* |
4212 | | * Humm, we are copying an element whose namespace is defined |
4213 | | * out of the new tree scope. Search it in the original tree |
4214 | | * and add it at the top of the new tree. |
4215 | | * |
4216 | | * TODO: Searching the original tree seems unnecessary. We |
4217 | | * already have a namespace URI. |
4218 | | */ |
4219 | 0 | res = xmlSearchNsSafe(node, node->ns->prefix, &ns); |
4220 | 0 | if (res < 0) |
4221 | 0 | goto error; |
4222 | 0 | if (ns != NULL) { |
4223 | 0 | xmlNodePtr root = ret; |
4224 | |
|
4225 | 0 | while (root->parent != NULL) root = root->parent; |
4226 | 0 | ret->ns = xmlNewNs(root, ns->href, ns->prefix); |
4227 | 0 | } else { |
4228 | 0 | ret->ns = xmlNewReconciledNs(ret, node->ns); |
4229 | 0 | } |
4230 | 0 | if (ret->ns == NULL) |
4231 | 0 | goto error; |
4232 | 0 | } else { |
4233 | | /* |
4234 | | * reference the existing namespace definition in our own tree. |
4235 | | */ |
4236 | 0 | ret->ns = ns; |
4237 | 0 | } |
4238 | 0 | } |
4239 | 0 | if ((node->type == XML_ELEMENT_NODE) && (node->properties != NULL)) { |
4240 | 0 | ret->properties = xmlCopyPropList(ret, node->properties); |
4241 | 0 | if (ret->properties == NULL) |
4242 | 0 | goto error; |
4243 | 0 | } |
4244 | 0 | if (node->type == XML_ENTITY_REF_NODE) { |
4245 | 0 | if ((doc == NULL) || (node->doc != doc)) { |
4246 | | /* |
4247 | | * The copied node will go into a separate document, so |
4248 | | * to avoid dangling references to the ENTITY_DECL node |
4249 | | * we cannot keep the reference. Try to find it in the |
4250 | | * target document. |
4251 | | */ |
4252 | 0 | ret->children = (xmlNodePtr) xmlGetDocEntity(doc, ret->name); |
4253 | 0 | } else { |
4254 | 0 | ret->children = node->children; |
4255 | 0 | } |
4256 | 0 | ret->last = ret->children; |
4257 | 0 | } else if ((node->children != NULL) && (extended != 2)) { |
4258 | 0 | xmlNodePtr cur, insert; |
4259 | |
|
4260 | 0 | cur = node->children; |
4261 | 0 | insert = ret; |
4262 | 0 | while (cur != NULL) { |
4263 | 0 | xmlNodePtr copy = xmlStaticCopyNode(cur, doc, insert, 2); |
4264 | 0 | if (copy == NULL) |
4265 | 0 | goto error; |
4266 | | |
4267 | | /* Check for coalesced text nodes */ |
4268 | 0 | if (insert->last != copy) { |
4269 | 0 | if (insert->last == NULL) { |
4270 | 0 | insert->children = copy; |
4271 | 0 | } else { |
4272 | 0 | copy->prev = insert->last; |
4273 | 0 | insert->last->next = copy; |
4274 | 0 | } |
4275 | 0 | insert->last = copy; |
4276 | 0 | } |
4277 | |
|
4278 | 0 | if ((cur->type != XML_ENTITY_REF_NODE) && |
4279 | 0 | (cur->children != NULL)) { |
4280 | 0 | cur = cur->children; |
4281 | 0 | insert = copy; |
4282 | 0 | continue; |
4283 | 0 | } |
4284 | | |
4285 | 0 | while (1) { |
4286 | 0 | if (cur->next != NULL) { |
4287 | 0 | cur = cur->next; |
4288 | 0 | break; |
4289 | 0 | } |
4290 | | |
4291 | 0 | cur = cur->parent; |
4292 | 0 | insert = insert->parent; |
4293 | 0 | if (cur == node) { |
4294 | 0 | cur = NULL; |
4295 | 0 | break; |
4296 | 0 | } |
4297 | 0 | } |
4298 | 0 | } |
4299 | 0 | } |
4300 | | |
4301 | 0 | out: |
4302 | 0 | if ((xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue)) |
4303 | 0 | xmlRegisterNodeDefaultValue((xmlNodePtr)ret); |
4304 | 0 | return(ret); |
4305 | | |
4306 | 0 | error: |
4307 | 0 | xmlFreeNode(ret); |
4308 | 0 | return(NULL); |
4309 | 0 | } |
4310 | | |
4311 | | /** |
4312 | | * xmlStaticCopyNodeList: |
4313 | | * @node: node to copy |
4314 | | * @doc: target document |
4315 | | * @parent: target node (optional) |
4316 | | * |
4317 | | * Copy a node list. If @parent is provided, sets the parent pointer |
4318 | | * of the copied nodes, but doesn't update the children and last |
4319 | | * pointer of @parent. |
4320 | | * |
4321 | | * Returns a the copy or NULL in case of error. |
4322 | | */ |
4323 | | xmlNodePtr |
4324 | 0 | xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) { |
4325 | 0 | xmlNodePtr ret = NULL; |
4326 | 0 | xmlNodePtr p = NULL,q; |
4327 | 0 | xmlDtdPtr newSubset = NULL; |
4328 | 0 | int linkedSubset = 0; |
4329 | |
|
4330 | 0 | while (node != NULL) { |
4331 | 0 | xmlNodePtr next = node->next; |
4332 | |
|
4333 | 0 | if (node->type == XML_DTD_NODE ) { |
4334 | 0 | if (doc == NULL) { |
4335 | 0 | node = next; |
4336 | 0 | continue; |
4337 | 0 | } |
4338 | 0 | if ((doc->intSubset == NULL) && (newSubset == NULL)) { |
4339 | 0 | q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node ); |
4340 | 0 | if (q == NULL) goto error; |
4341 | | /* Can't fail on DTD */ |
4342 | 0 | xmlSetTreeDoc(q, doc); |
4343 | 0 | q->parent = parent; |
4344 | 0 | newSubset = (xmlDtdPtr) q; |
4345 | 0 | } else { |
4346 | | /* |
4347 | | * We don't allow multiple internal subsets in a document, |
4348 | | * so we move the DTD instead of creating a copy. |
4349 | | */ |
4350 | 0 | linkedSubset = 1; |
4351 | 0 | q = (xmlNodePtr) doc->intSubset; |
4352 | | /* Unlink */ |
4353 | 0 | if (q->prev == NULL) { |
4354 | 0 | if (q->parent != NULL) |
4355 | 0 | q->parent->children = q->next; |
4356 | 0 | } else { |
4357 | 0 | q->prev->next = q->next; |
4358 | 0 | } |
4359 | 0 | if (q->next == NULL) { |
4360 | 0 | if (q->parent != NULL) |
4361 | 0 | q->parent->last = q->prev; |
4362 | 0 | } else { |
4363 | 0 | q->next->prev = q->prev; |
4364 | 0 | } |
4365 | 0 | q->parent = parent; |
4366 | 0 | q->next = NULL; |
4367 | 0 | q->prev = NULL; |
4368 | 0 | } |
4369 | 0 | } else |
4370 | 0 | q = xmlStaticCopyNode(node, doc, parent, 1); |
4371 | 0 | if (q == NULL) goto error; |
4372 | 0 | if (ret == NULL) { |
4373 | 0 | q->prev = NULL; |
4374 | 0 | ret = p = q; |
4375 | 0 | } else if (p != q) { |
4376 | | /* the test is required if xmlStaticCopyNode coalesced 2 text nodes */ |
4377 | 0 | p->next = q; |
4378 | 0 | q->prev = p; |
4379 | 0 | p = q; |
4380 | 0 | } |
4381 | 0 | node = next; |
4382 | 0 | } |
4383 | 0 | if ((doc != NULL) && (newSubset != NULL)) |
4384 | 0 | doc->intSubset = newSubset; |
4385 | 0 | return(ret); |
4386 | 0 | error: |
4387 | 0 | xmlFreeNodeList(ret); |
4388 | 0 | if (newSubset != NULL) |
4389 | 0 | xmlFreeDtd(newSubset); |
4390 | 0 | if (linkedSubset != 0) { |
4391 | 0 | doc->intSubset->next = NULL; |
4392 | 0 | doc->intSubset->prev = NULL; |
4393 | 0 | } |
4394 | 0 | return(NULL); |
4395 | 0 | } |
4396 | | |
4397 | | /** |
4398 | | * xmlCopyNode: |
4399 | | * @node: the node |
4400 | | * @extended: if 1 do a recursive copy (properties, namespaces and children |
4401 | | * when applicable) |
4402 | | * if 2 copy properties and namespaces (when applicable) |
4403 | | * |
4404 | | * Copy a node. |
4405 | | * |
4406 | | * Use of this function is DISCOURAGED in favor of xmlDocCopyNode. |
4407 | | * |
4408 | | * Returns the copied node or NULL if a memory allocation failed. |
4409 | | */ |
4410 | | xmlNodePtr |
4411 | 0 | xmlCopyNode(xmlNodePtr node, int extended) { |
4412 | 0 | xmlNodePtr ret; |
4413 | |
|
4414 | 0 | ret = xmlStaticCopyNode(node, NULL, NULL, extended); |
4415 | 0 | return(ret); |
4416 | 0 | } |
4417 | | |
4418 | | /** |
4419 | | * xmlDocCopyNode: |
4420 | | * @node: the node |
4421 | | * @doc: the document |
4422 | | * @extended: if 1 do a recursive copy (properties, namespaces and children |
4423 | | * when applicable) |
4424 | | * if 2 copy properties and namespaces (when applicable) |
4425 | | * |
4426 | | * Copy a node into another document. |
4427 | | * |
4428 | | * Returns the copied node or NULL if a memory allocation failed. |
4429 | | */ |
4430 | | xmlNodePtr |
4431 | 0 | xmlDocCopyNode(xmlNodePtr node, xmlDocPtr doc, int extended) { |
4432 | 0 | xmlNodePtr ret; |
4433 | |
|
4434 | 0 | ret = xmlStaticCopyNode(node, doc, NULL, extended); |
4435 | 0 | return(ret); |
4436 | 0 | } |
4437 | | |
4438 | | /** |
4439 | | * xmlDocCopyNodeList: |
4440 | | * @doc: the target document |
4441 | | * @node: the first node in the list. |
4442 | | * |
4443 | | * Copy a node list and all children into a new document. |
4444 | | * |
4445 | | * Returns the head of the copied list or NULL if a memory |
4446 | | * allocation failed. |
4447 | | */ |
4448 | 0 | xmlNodePtr xmlDocCopyNodeList(xmlDocPtr doc, xmlNodePtr node) { |
4449 | 0 | xmlNodePtr ret = xmlStaticCopyNodeList(node, doc, NULL); |
4450 | 0 | return(ret); |
4451 | 0 | } |
4452 | | |
4453 | | /** |
4454 | | * xmlCopyNodeList: |
4455 | | * @node: the first node in the list. |
4456 | | * |
4457 | | * Copy a node list and all children. |
4458 | | * |
4459 | | * Use of this function is DISCOURAGED in favor of xmlDocCopyNodeList. |
4460 | | * |
4461 | | * Returns the head of the copied list or NULL if a memory |
4462 | | * allocation failed. |
4463 | | */ |
4464 | 0 | xmlNodePtr xmlCopyNodeList(xmlNodePtr node) { |
4465 | 0 | xmlNodePtr ret = xmlStaticCopyNodeList(node, NULL, NULL); |
4466 | 0 | return(ret); |
4467 | 0 | } |
4468 | | |
4469 | | /** |
4470 | | * xmlCopyDtd: |
4471 | | * @dtd: the DTD |
4472 | | * |
4473 | | * Copy a DTD. |
4474 | | * |
4475 | | * Returns the copied DTD or NULL if a memory allocation failed. |
4476 | | */ |
4477 | | xmlDtdPtr |
4478 | 0 | xmlCopyDtd(xmlDtdPtr dtd) { |
4479 | 0 | xmlDtdPtr ret; |
4480 | 0 | xmlNodePtr cur, p = NULL, q; |
4481 | |
|
4482 | 0 | if (dtd == NULL) return(NULL); |
4483 | 0 | ret = xmlNewDtd(NULL, dtd->name, dtd->ExternalID, dtd->SystemID); |
4484 | 0 | if (ret == NULL) return(NULL); |
4485 | 0 | if (dtd->entities != NULL) { |
4486 | 0 | ret->entities = (void *) xmlCopyEntitiesTable( |
4487 | 0 | (xmlEntitiesTablePtr) dtd->entities); |
4488 | 0 | if (ret->entities == NULL) |
4489 | 0 | goto error; |
4490 | 0 | } |
4491 | 0 | if (dtd->notations != NULL) { |
4492 | 0 | ret->notations = (void *) xmlCopyNotationTable( |
4493 | 0 | (xmlNotationTablePtr) dtd->notations); |
4494 | 0 | if (ret->notations == NULL) |
4495 | 0 | goto error; |
4496 | 0 | } |
4497 | 0 | if (dtd->elements != NULL) { |
4498 | 0 | ret->elements = (void *) xmlCopyElementTable( |
4499 | 0 | (xmlElementTablePtr) dtd->elements); |
4500 | 0 | if (ret->elements == NULL) |
4501 | 0 | goto error; |
4502 | 0 | } |
4503 | 0 | if (dtd->attributes != NULL) { |
4504 | 0 | ret->attributes = (void *) xmlCopyAttributeTable( |
4505 | 0 | (xmlAttributeTablePtr) dtd->attributes); |
4506 | 0 | if (ret->attributes == NULL) |
4507 | 0 | goto error; |
4508 | 0 | } |
4509 | 0 | if (dtd->pentities != NULL) { |
4510 | 0 | ret->pentities = (void *) xmlCopyEntitiesTable( |
4511 | 0 | (xmlEntitiesTablePtr) dtd->pentities); |
4512 | 0 | if (ret->pentities == NULL) |
4513 | 0 | goto error; |
4514 | 0 | } |
4515 | | |
4516 | 0 | cur = dtd->children; |
4517 | 0 | while (cur != NULL) { |
4518 | 0 | q = NULL; |
4519 | |
|
4520 | 0 | if (cur->type == XML_ENTITY_DECL) { |
4521 | 0 | xmlEntityPtr tmp = (xmlEntityPtr) cur; |
4522 | 0 | switch (tmp->etype) { |
4523 | 0 | case XML_INTERNAL_GENERAL_ENTITY: |
4524 | 0 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
4525 | 0 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
4526 | 0 | q = (xmlNodePtr) xmlGetEntityFromDtd(ret, tmp->name); |
4527 | 0 | break; |
4528 | 0 | case XML_INTERNAL_PARAMETER_ENTITY: |
4529 | 0 | case XML_EXTERNAL_PARAMETER_ENTITY: |
4530 | 0 | q = (xmlNodePtr) |
4531 | 0 | xmlGetParameterEntityFromDtd(ret, tmp->name); |
4532 | 0 | break; |
4533 | 0 | case XML_INTERNAL_PREDEFINED_ENTITY: |
4534 | 0 | break; |
4535 | 0 | } |
4536 | 0 | } else if (cur->type == XML_ELEMENT_DECL) { |
4537 | 0 | xmlElementPtr tmp = (xmlElementPtr) cur; |
4538 | 0 | q = (xmlNodePtr) |
4539 | 0 | xmlGetDtdQElementDesc(ret, tmp->name, tmp->prefix); |
4540 | 0 | } else if (cur->type == XML_ATTRIBUTE_DECL) { |
4541 | 0 | xmlAttributePtr tmp = (xmlAttributePtr) cur; |
4542 | 0 | q = (xmlNodePtr) |
4543 | 0 | xmlGetDtdQAttrDesc(ret, tmp->elem, tmp->name, tmp->prefix); |
4544 | 0 | } else if (cur->type == XML_COMMENT_NODE) { |
4545 | 0 | q = xmlCopyNode(cur, 0); |
4546 | 0 | if (q == NULL) |
4547 | 0 | goto error; |
4548 | 0 | } |
4549 | | |
4550 | 0 | if (q == NULL) { |
4551 | 0 | cur = cur->next; |
4552 | 0 | continue; |
4553 | 0 | } |
4554 | | |
4555 | 0 | if (p == NULL) |
4556 | 0 | ret->children = q; |
4557 | 0 | else |
4558 | 0 | p->next = q; |
4559 | |
|
4560 | 0 | q->prev = p; |
4561 | 0 | q->parent = (xmlNodePtr) ret; |
4562 | 0 | q->next = NULL; |
4563 | 0 | ret->last = q; |
4564 | 0 | p = q; |
4565 | 0 | cur = cur->next; |
4566 | 0 | } |
4567 | | |
4568 | 0 | return(ret); |
4569 | | |
4570 | 0 | error: |
4571 | 0 | xmlFreeDtd(ret); |
4572 | 0 | return(NULL); |
4573 | 0 | } |
4574 | | |
4575 | | /** |
4576 | | * xmlCopyDoc: |
4577 | | * @doc: the document |
4578 | | * @recursive: if not zero do a recursive copy. |
4579 | | * |
4580 | | * Copy a document. If recursive, the content tree will |
4581 | | * be copied too as well as DTD, namespaces and entities. |
4582 | | * |
4583 | | * Returns the copied document or NULL if a memory allocation |
4584 | | * failed. |
4585 | | */ |
4586 | | xmlDocPtr |
4587 | 0 | xmlCopyDoc(xmlDocPtr doc, int recursive) { |
4588 | 0 | xmlDocPtr ret; |
4589 | |
|
4590 | 0 | if (doc == NULL) return(NULL); |
4591 | 0 | ret = xmlNewDoc(doc->version); |
4592 | 0 | if (ret == NULL) return(NULL); |
4593 | 0 | ret->type = doc->type; |
4594 | 0 | if (doc->name != NULL) { |
4595 | 0 | ret->name = xmlMemStrdup(doc->name); |
4596 | 0 | if (ret->name == NULL) |
4597 | 0 | goto error; |
4598 | 0 | } |
4599 | 0 | if (doc->encoding != NULL) { |
4600 | 0 | ret->encoding = xmlStrdup(doc->encoding); |
4601 | 0 | if (ret->encoding == NULL) |
4602 | 0 | goto error; |
4603 | 0 | } |
4604 | 0 | if (doc->URL != NULL) { |
4605 | 0 | ret->URL = xmlStrdup(doc->URL); |
4606 | 0 | if (ret->URL == NULL) |
4607 | 0 | goto error; |
4608 | 0 | } |
4609 | 0 | ret->charset = doc->charset; |
4610 | 0 | ret->compression = doc->compression; |
4611 | 0 | ret->standalone = doc->standalone; |
4612 | 0 | if (!recursive) return(ret); |
4613 | | |
4614 | 0 | ret->last = NULL; |
4615 | 0 | ret->children = NULL; |
4616 | 0 | if (doc->intSubset != NULL) { |
4617 | 0 | ret->intSubset = xmlCopyDtd(doc->intSubset); |
4618 | 0 | if (ret->intSubset == NULL) |
4619 | 0 | goto error; |
4620 | | /* Can't fail on DTD */ |
4621 | 0 | xmlSetTreeDoc((xmlNodePtr)ret->intSubset, ret); |
4622 | 0 | } |
4623 | 0 | if (doc->oldNs != NULL) { |
4624 | 0 | ret->oldNs = xmlCopyNamespaceList(doc->oldNs); |
4625 | 0 | if (ret->oldNs == NULL) |
4626 | 0 | goto error; |
4627 | 0 | } |
4628 | 0 | if (doc->children != NULL) { |
4629 | 0 | xmlNodePtr tmp; |
4630 | |
|
4631 | 0 | ret->children = xmlStaticCopyNodeList(doc->children, ret, |
4632 | 0 | (xmlNodePtr)ret); |
4633 | 0 | if (ret->children == NULL) |
4634 | 0 | goto error; |
4635 | 0 | ret->last = NULL; |
4636 | 0 | tmp = ret->children; |
4637 | 0 | while (tmp != NULL) { |
4638 | 0 | if (tmp->next == NULL) |
4639 | 0 | ret->last = tmp; |
4640 | 0 | tmp = tmp->next; |
4641 | 0 | } |
4642 | 0 | } |
4643 | 0 | return(ret); |
4644 | | |
4645 | 0 | error: |
4646 | 0 | xmlFreeDoc(ret); |
4647 | 0 | return(NULL); |
4648 | 0 | } |
4649 | | |
4650 | | /************************************************************************ |
4651 | | * * |
4652 | | * Content access functions * |
4653 | | * * |
4654 | | ************************************************************************/ |
4655 | | |
4656 | | /** |
4657 | | * xmlGetLineNoInternal: |
4658 | | * @node: valid node |
4659 | | * @depth: used to limit any risk of recursion |
4660 | | * |
4661 | | * Get line number of @node. |
4662 | | * Try to override the limitation of lines being store in 16 bits ints |
4663 | | * |
4664 | | * Returns the line number if successful, -1 otherwise |
4665 | | */ |
4666 | | static long |
4667 | | xmlGetLineNoInternal(const xmlNode *node, int depth) |
4668 | 2.87k | { |
4669 | 2.87k | long result = -1; |
4670 | | |
4671 | 2.87k | if (depth >= 5) |
4672 | 76 | return(-1); |
4673 | | |
4674 | 2.79k | if (!node) |
4675 | 0 | return result; |
4676 | 2.79k | if ((node->type == XML_ELEMENT_NODE) || |
4677 | 2.79k | (node->type == XML_TEXT_NODE) || |
4678 | 2.79k | (node->type == XML_COMMENT_NODE) || |
4679 | 2.79k | (node->type == XML_PI_NODE)) { |
4680 | 2.79k | if (node->line == 65535) { |
4681 | 453 | if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL)) |
4682 | 0 | result = (long) (ptrdiff_t) node->psvi; |
4683 | 453 | else if ((node->type == XML_ELEMENT_NODE) && |
4684 | 453 | (node->children != NULL)) |
4685 | 0 | result = xmlGetLineNoInternal(node->children, depth + 1); |
4686 | 453 | else if (node->next != NULL) |
4687 | 152 | result = xmlGetLineNoInternal(node->next, depth + 1); |
4688 | 301 | else if (node->prev != NULL) |
4689 | 244 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
4690 | 453 | } |
4691 | 2.79k | if ((result == -1) || (result == 65535)) |
4692 | 2.78k | result = (long) node->line; |
4693 | 2.79k | } else if ((node->prev != NULL) && |
4694 | 0 | ((node->prev->type == XML_ELEMENT_NODE) || |
4695 | 0 | (node->prev->type == XML_TEXT_NODE) || |
4696 | 0 | (node->prev->type == XML_COMMENT_NODE) || |
4697 | 0 | (node->prev->type == XML_PI_NODE))) |
4698 | 0 | result = xmlGetLineNoInternal(node->prev, depth + 1); |
4699 | 0 | else if ((node->parent != NULL) && |
4700 | 0 | (node->parent->type == XML_ELEMENT_NODE)) |
4701 | 0 | result = xmlGetLineNoInternal(node->parent, depth + 1); |
4702 | | |
4703 | 2.79k | return result; |
4704 | 2.79k | } |
4705 | | |
4706 | | /** |
4707 | | * xmlGetLineNo: |
4708 | | * @node: valid node |
4709 | | * |
4710 | | * Get line number of @node. |
4711 | | * Try to override the limitation of lines being store in 16 bits ints |
4712 | | * if XML_PARSE_BIG_LINES parser option was used |
4713 | | * |
4714 | | * Returns the line number if successful, -1 otherwise |
4715 | | */ |
4716 | | long |
4717 | | xmlGetLineNo(const xmlNode *node) |
4718 | 2.47k | { |
4719 | 2.47k | return(xmlGetLineNoInternal(node, 0)); |
4720 | 2.47k | } |
4721 | | |
4722 | | /** |
4723 | | * xmlGetNodePath: |
4724 | | * @node: a node |
4725 | | * |
4726 | | * Build a structure based Path for the given node |
4727 | | * |
4728 | | * Returns the new path or NULL in case of error. The caller must free |
4729 | | * the returned string |
4730 | | */ |
4731 | | xmlChar * |
4732 | | xmlGetNodePath(const xmlNode *node) |
4733 | 0 | { |
4734 | 0 | const xmlNode *cur, *tmp, *next; |
4735 | 0 | xmlChar *buffer = NULL, *temp; |
4736 | 0 | size_t buf_len; |
4737 | 0 | xmlChar *buf; |
4738 | 0 | const char *sep; |
4739 | 0 | const char *name; |
4740 | 0 | char nametemp[100]; |
4741 | 0 | int occur = 0, generic; |
4742 | |
|
4743 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
4744 | 0 | return (NULL); |
4745 | | |
4746 | 0 | buf_len = 500; |
4747 | 0 | buffer = xmlMalloc(buf_len); |
4748 | 0 | if (buffer == NULL) |
4749 | 0 | return (NULL); |
4750 | 0 | buf = xmlMalloc(buf_len); |
4751 | 0 | if (buf == NULL) { |
4752 | 0 | xmlFree(buffer); |
4753 | 0 | return (NULL); |
4754 | 0 | } |
4755 | | |
4756 | 0 | buffer[0] = 0; |
4757 | 0 | cur = node; |
4758 | 0 | do { |
4759 | 0 | name = ""; |
4760 | 0 | sep = "?"; |
4761 | 0 | occur = 0; |
4762 | 0 | if ((cur->type == XML_DOCUMENT_NODE) || |
4763 | 0 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
4764 | 0 | if (buffer[0] == '/') |
4765 | 0 | break; |
4766 | 0 | sep = "/"; |
4767 | 0 | next = NULL; |
4768 | 0 | } else if (cur->type == XML_ELEMENT_NODE) { |
4769 | 0 | generic = 0; |
4770 | 0 | sep = "/"; |
4771 | 0 | name = (const char *) cur->name; |
4772 | 0 | if (cur->ns) { |
4773 | 0 | if (cur->ns->prefix != NULL) { |
4774 | 0 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
4775 | 0 | (char *)cur->ns->prefix, (char *)cur->name); |
4776 | 0 | nametemp[sizeof(nametemp) - 1] = 0; |
4777 | 0 | name = nametemp; |
4778 | 0 | } else { |
4779 | | /* |
4780 | | * We cannot express named elements in the default |
4781 | | * namespace, so use "*". |
4782 | | */ |
4783 | 0 | generic = 1; |
4784 | 0 | name = "*"; |
4785 | 0 | } |
4786 | 0 | } |
4787 | 0 | next = cur->parent; |
4788 | | |
4789 | | /* |
4790 | | * Thumbler index computation |
4791 | | * TODO: the occurrence test seems bogus for namespaced names |
4792 | | */ |
4793 | 0 | tmp = cur->prev; |
4794 | 0 | while (tmp != NULL) { |
4795 | 0 | if ((tmp->type == XML_ELEMENT_NODE) && |
4796 | 0 | (generic || |
4797 | 0 | (xmlStrEqual(cur->name, tmp->name) && |
4798 | 0 | ((tmp->ns == cur->ns) || |
4799 | 0 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
4800 | 0 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
4801 | 0 | occur++; |
4802 | 0 | tmp = tmp->prev; |
4803 | 0 | } |
4804 | 0 | if (occur == 0) { |
4805 | 0 | tmp = cur->next; |
4806 | 0 | while (tmp != NULL && occur == 0) { |
4807 | 0 | if ((tmp->type == XML_ELEMENT_NODE) && |
4808 | 0 | (generic || |
4809 | 0 | (xmlStrEqual(cur->name, tmp->name) && |
4810 | 0 | ((tmp->ns == cur->ns) || |
4811 | 0 | ((tmp->ns != NULL) && (cur->ns != NULL) && |
4812 | 0 | (xmlStrEqual(cur->ns->prefix, tmp->ns->prefix))))))) |
4813 | 0 | occur++; |
4814 | 0 | tmp = tmp->next; |
4815 | 0 | } |
4816 | 0 | if (occur != 0) |
4817 | 0 | occur = 1; |
4818 | 0 | } else |
4819 | 0 | occur++; |
4820 | 0 | } else if (cur->type == XML_COMMENT_NODE) { |
4821 | 0 | sep = "/"; |
4822 | 0 | name = "comment()"; |
4823 | 0 | next = cur->parent; |
4824 | | |
4825 | | /* |
4826 | | * Thumbler index computation |
4827 | | */ |
4828 | 0 | tmp = cur->prev; |
4829 | 0 | while (tmp != NULL) { |
4830 | 0 | if (tmp->type == XML_COMMENT_NODE) |
4831 | 0 | occur++; |
4832 | 0 | tmp = tmp->prev; |
4833 | 0 | } |
4834 | 0 | if (occur == 0) { |
4835 | 0 | tmp = cur->next; |
4836 | 0 | while (tmp != NULL && occur == 0) { |
4837 | 0 | if (tmp->type == XML_COMMENT_NODE) |
4838 | 0 | occur++; |
4839 | 0 | tmp = tmp->next; |
4840 | 0 | } |
4841 | 0 | if (occur != 0) |
4842 | 0 | occur = 1; |
4843 | 0 | } else |
4844 | 0 | occur++; |
4845 | 0 | } else if ((cur->type == XML_TEXT_NODE) || |
4846 | 0 | (cur->type == XML_CDATA_SECTION_NODE)) { |
4847 | 0 | sep = "/"; |
4848 | 0 | name = "text()"; |
4849 | 0 | next = cur->parent; |
4850 | | |
4851 | | /* |
4852 | | * Thumbler index computation |
4853 | | */ |
4854 | 0 | tmp = cur->prev; |
4855 | 0 | while (tmp != NULL) { |
4856 | 0 | if ((tmp->type == XML_TEXT_NODE) || |
4857 | 0 | (tmp->type == XML_CDATA_SECTION_NODE)) |
4858 | 0 | occur++; |
4859 | 0 | tmp = tmp->prev; |
4860 | 0 | } |
4861 | | /* |
4862 | | * Evaluate if this is the only text- or CDATA-section-node; |
4863 | | * if yes, then we'll get "text()", otherwise "text()[1]". |
4864 | | */ |
4865 | 0 | if (occur == 0) { |
4866 | 0 | tmp = cur->next; |
4867 | 0 | while (tmp != NULL) { |
4868 | 0 | if ((tmp->type == XML_TEXT_NODE) || |
4869 | 0 | (tmp->type == XML_CDATA_SECTION_NODE)) |
4870 | 0 | { |
4871 | 0 | occur = 1; |
4872 | 0 | break; |
4873 | 0 | } |
4874 | 0 | tmp = tmp->next; |
4875 | 0 | } |
4876 | 0 | } else |
4877 | 0 | occur++; |
4878 | 0 | } else if (cur->type == XML_PI_NODE) { |
4879 | 0 | sep = "/"; |
4880 | 0 | snprintf(nametemp, sizeof(nametemp) - 1, |
4881 | 0 | "processing-instruction('%s')", (char *)cur->name); |
4882 | 0 | nametemp[sizeof(nametemp) - 1] = 0; |
4883 | 0 | name = nametemp; |
4884 | |
|
4885 | 0 | next = cur->parent; |
4886 | | |
4887 | | /* |
4888 | | * Thumbler index computation |
4889 | | */ |
4890 | 0 | tmp = cur->prev; |
4891 | 0 | while (tmp != NULL) { |
4892 | 0 | if ((tmp->type == XML_PI_NODE) && |
4893 | 0 | (xmlStrEqual(cur->name, tmp->name))) |
4894 | 0 | occur++; |
4895 | 0 | tmp = tmp->prev; |
4896 | 0 | } |
4897 | 0 | if (occur == 0) { |
4898 | 0 | tmp = cur->next; |
4899 | 0 | while (tmp != NULL && occur == 0) { |
4900 | 0 | if ((tmp->type == XML_PI_NODE) && |
4901 | 0 | (xmlStrEqual(cur->name, tmp->name))) |
4902 | 0 | occur++; |
4903 | 0 | tmp = tmp->next; |
4904 | 0 | } |
4905 | 0 | if (occur != 0) |
4906 | 0 | occur = 1; |
4907 | 0 | } else |
4908 | 0 | occur++; |
4909 | |
|
4910 | 0 | } else if (cur->type == XML_ATTRIBUTE_NODE) { |
4911 | 0 | sep = "/@"; |
4912 | 0 | name = (const char *) (((xmlAttrPtr) cur)->name); |
4913 | 0 | if (cur->ns) { |
4914 | 0 | if (cur->ns->prefix != NULL) |
4915 | 0 | snprintf(nametemp, sizeof(nametemp) - 1, "%s:%s", |
4916 | 0 | (char *)cur->ns->prefix, (char *)cur->name); |
4917 | 0 | else |
4918 | 0 | snprintf(nametemp, sizeof(nametemp) - 1, "%s", |
4919 | 0 | (char *)cur->name); |
4920 | 0 | nametemp[sizeof(nametemp) - 1] = 0; |
4921 | 0 | name = nametemp; |
4922 | 0 | } |
4923 | 0 | next = ((xmlAttrPtr) cur)->parent; |
4924 | 0 | } else { |
4925 | 0 | xmlFree(buf); |
4926 | 0 | xmlFree(buffer); |
4927 | 0 | return (NULL); |
4928 | 0 | } |
4929 | | |
4930 | | /* |
4931 | | * Make sure there is enough room |
4932 | | */ |
4933 | 0 | if (xmlStrlen(buffer) + sizeof(nametemp) + 20 > buf_len) { |
4934 | 0 | buf_len = |
4935 | 0 | 2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20; |
4936 | 0 | temp = (xmlChar *) xmlRealloc(buffer, buf_len); |
4937 | 0 | if (temp == NULL) { |
4938 | 0 | xmlFree(buf); |
4939 | 0 | xmlFree(buffer); |
4940 | 0 | return (NULL); |
4941 | 0 | } |
4942 | 0 | buffer = temp; |
4943 | 0 | temp = (xmlChar *) xmlRealloc(buf, buf_len); |
4944 | 0 | if (temp == NULL) { |
4945 | 0 | xmlFree(buf); |
4946 | 0 | xmlFree(buffer); |
4947 | 0 | return (NULL); |
4948 | 0 | } |
4949 | 0 | buf = temp; |
4950 | 0 | } |
4951 | 0 | if (occur == 0) |
4952 | 0 | snprintf((char *) buf, buf_len, "%s%s%s", |
4953 | 0 | sep, name, (char *) buffer); |
4954 | 0 | else |
4955 | 0 | snprintf((char *) buf, buf_len, "%s%s[%d]%s", |
4956 | 0 | sep, name, occur, (char *) buffer); |
4957 | 0 | snprintf((char *) buffer, buf_len, "%s", (char *)buf); |
4958 | 0 | cur = next; |
4959 | 0 | } while (cur != NULL); |
4960 | 0 | xmlFree(buf); |
4961 | 0 | return (buffer); |
4962 | 0 | } |
4963 | | |
4964 | | /** |
4965 | | * xmlDocGetRootElement: |
4966 | | * @doc: the document |
4967 | | * |
4968 | | * Get the root element of the document (doc->children is a list |
4969 | | * containing possibly comments, PIs, etc ...). |
4970 | | * |
4971 | | * Returns the root element or NULL if no element was found. |
4972 | | */ |
4973 | | xmlNodePtr |
4974 | 9.91k | xmlDocGetRootElement(const xmlDoc *doc) { |
4975 | 9.91k | xmlNodePtr ret; |
4976 | | |
4977 | 9.91k | if (doc == NULL) return(NULL); |
4978 | 36 | ret = doc->children; |
4979 | 200 | while (ret != NULL) { |
4980 | 200 | if (ret->type == XML_ELEMENT_NODE) |
4981 | 36 | return(ret); |
4982 | 164 | ret = ret->next; |
4983 | 164 | } |
4984 | 0 | return(ret); |
4985 | 36 | } |
4986 | | |
4987 | | /** |
4988 | | * xmlDocSetRootElement: |
4989 | | * @doc: the document |
4990 | | * @root: the new document root element, if root is NULL no action is taken, |
4991 | | * to remove a node from a document use xmlUnlinkNode(root) instead. |
4992 | | * |
4993 | | * Set the root element of the document (doc->children is a list |
4994 | | * containing possibly comments, PIs, etc ...). |
4995 | | * |
4996 | | * @root must be an element node. It is unlinked before insertion. |
4997 | | * |
4998 | | * Returns the unlinked old root element or NULL if the document |
4999 | | * didn't have a root element or a memory allocation failed. |
5000 | | */ |
5001 | | xmlNodePtr |
5002 | 0 | xmlDocSetRootElement(xmlDocPtr doc, xmlNodePtr root) { |
5003 | 0 | xmlNodePtr old = NULL; |
5004 | |
|
5005 | 0 | if (doc == NULL) return(NULL); |
5006 | 0 | if ((root == NULL) || (root->type == XML_NAMESPACE_DECL)) |
5007 | 0 | return(NULL); |
5008 | 0 | old = doc->children; |
5009 | 0 | while (old != NULL) { |
5010 | 0 | if (old->type == XML_ELEMENT_NODE) |
5011 | 0 | break; |
5012 | 0 | old = old->next; |
5013 | 0 | } |
5014 | 0 | if (old == root) |
5015 | 0 | return(old); |
5016 | 0 | xmlUnlinkNodeInternal(root); |
5017 | 0 | if (xmlSetTreeDoc(root, doc) < 0) |
5018 | 0 | return(NULL); |
5019 | 0 | root->parent = (xmlNodePtr) doc; |
5020 | 0 | if (old == NULL) { |
5021 | 0 | if (doc->children == NULL) { |
5022 | 0 | doc->children = root; |
5023 | 0 | doc->last = root; |
5024 | 0 | } else { |
5025 | 0 | xmlAddSibling(doc->children, root); |
5026 | 0 | } |
5027 | 0 | } else { |
5028 | 0 | xmlReplaceNode(old, root); |
5029 | 0 | } |
5030 | 0 | return(old); |
5031 | 0 | } |
5032 | | |
5033 | | /** |
5034 | | * xmlNodeSetLang: |
5035 | | * @cur: the node being changed |
5036 | | * @lang: the language description |
5037 | | * |
5038 | | * Set the language of a node, i.e. the values of the xml:lang |
5039 | | * attribute. |
5040 | | * |
5041 | | * Return 0 on success, 1 if arguments are invalid, -1 if a |
5042 | | * memory allocation failed. |
5043 | | */ |
5044 | | int |
5045 | 0 | xmlNodeSetLang(xmlNodePtr cur, const xmlChar *lang) { |
5046 | 0 | xmlNsPtr ns; |
5047 | 0 | xmlAttrPtr attr; |
5048 | 0 | int res; |
5049 | |
|
5050 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
5051 | 0 | return(1); |
5052 | | |
5053 | 0 | res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
5054 | 0 | if (res != 0) |
5055 | 0 | return(res); |
5056 | 0 | attr = xmlSetNsProp(cur, ns, BAD_CAST "lang", lang); |
5057 | 0 | if (attr == NULL) |
5058 | 0 | return(-1); |
5059 | | |
5060 | 0 | return(0); |
5061 | 0 | } |
5062 | | |
5063 | | /** |
5064 | | * xmlNodeGetLang: |
5065 | | * @cur: the node being checked |
5066 | | * |
5067 | | * Searches the language of a node, i.e. the values of the xml:lang |
5068 | | * attribute or the one carried by the nearest ancestor. |
5069 | | * |
5070 | | * Returns a pointer to the lang value, or NULL if not found |
5071 | | * It's up to the caller to free the memory with xmlFree(). |
5072 | | */ |
5073 | | xmlChar * |
5074 | 0 | xmlNodeGetLang(const xmlNode *cur) { |
5075 | 0 | xmlChar *lang; |
5076 | 0 | int res; |
5077 | |
|
5078 | 0 | if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) |
5079 | 0 | return(NULL); |
5080 | | |
5081 | 0 | while (cur != NULL) { |
5082 | 0 | res = xmlNodeGetAttrValue(cur, BAD_CAST "lang", XML_XML_NAMESPACE, |
5083 | 0 | &lang); |
5084 | 0 | if (res < 0) |
5085 | 0 | return(NULL); |
5086 | 0 | if (lang != NULL) |
5087 | 0 | return(lang); |
5088 | | |
5089 | 0 | cur = cur->parent; |
5090 | 0 | } |
5091 | | |
5092 | 0 | return(NULL); |
5093 | 0 | } |
5094 | | |
5095 | | |
5096 | | /** |
5097 | | * xmlNodeSetSpacePreserve: |
5098 | | * @cur: the node being changed |
5099 | | * @val: the xml:space value ("0": default, 1: "preserve") |
5100 | | * |
5101 | | * Set (or reset) the space preserving behaviour of a node, i.e. the |
5102 | | * value of the xml:space attribute. |
5103 | | * |
5104 | | * Return 0 on success, 1 if arguments are invalid, -1 if a |
5105 | | * memory allocation failed. |
5106 | | */ |
5107 | | int |
5108 | 0 | xmlNodeSetSpacePreserve(xmlNodePtr cur, int val) { |
5109 | 0 | xmlNsPtr ns; |
5110 | 0 | xmlAttrPtr attr; |
5111 | 0 | const char *string; |
5112 | 0 | int res; |
5113 | |
|
5114 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
5115 | 0 | return(1); |
5116 | | |
5117 | 0 | res = xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
5118 | 0 | if (res != 0) |
5119 | 0 | return(res); |
5120 | | |
5121 | 0 | if (val == 0) |
5122 | 0 | string = "default"; |
5123 | 0 | else |
5124 | 0 | string = "preserve"; |
5125 | |
|
5126 | 0 | attr = xmlSetNsProp(cur, ns, BAD_CAST "space", BAD_CAST string); |
5127 | 0 | if (attr == NULL) |
5128 | 0 | return(-1); |
5129 | | |
5130 | 0 | return(0); |
5131 | 0 | } |
5132 | | |
5133 | | /** |
5134 | | * xmlNodeGetSpacePreserve: |
5135 | | * @cur: the node being checked |
5136 | | * |
5137 | | * Searches the space preserving behaviour of a node, i.e. the values |
5138 | | * of the xml:space attribute or the one carried by the nearest |
5139 | | * ancestor. |
5140 | | * |
5141 | | * Returns -1 if xml:space is not inherited, 0 if "default", 1 if "preserve" |
5142 | | */ |
5143 | | int |
5144 | 0 | xmlNodeGetSpacePreserve(const xmlNode *cur) { |
5145 | 0 | xmlChar *space; |
5146 | 0 | int res; |
5147 | |
|
5148 | 0 | if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) |
5149 | 0 | return(-1); |
5150 | | |
5151 | 0 | while (cur != NULL) { |
5152 | 0 | res = xmlNodeGetAttrValue(cur, BAD_CAST "space", XML_XML_NAMESPACE, |
5153 | 0 | &space); |
5154 | 0 | if (res < 0) |
5155 | 0 | return(-1); |
5156 | 0 | if (space != NULL) { |
5157 | 0 | if (xmlStrEqual(space, BAD_CAST "preserve")) { |
5158 | 0 | xmlFree(space); |
5159 | 0 | return(1); |
5160 | 0 | } |
5161 | 0 | if (xmlStrEqual(space, BAD_CAST "default")) { |
5162 | 0 | xmlFree(space); |
5163 | 0 | return(0); |
5164 | 0 | } |
5165 | 0 | xmlFree(space); |
5166 | 0 | } |
5167 | | |
5168 | 0 | cur = cur->parent; |
5169 | 0 | } |
5170 | | |
5171 | 0 | return(-1); |
5172 | 0 | } |
5173 | | |
5174 | | /** |
5175 | | * xmlNodeSetName: |
5176 | | * @cur: the node being changed |
5177 | | * @name: the new tag name |
5178 | | * |
5179 | | * Set (or reset) the name of a node. |
5180 | | */ |
5181 | | void |
5182 | 0 | xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) { |
5183 | 0 | xmlDocPtr doc; |
5184 | 0 | xmlDictPtr dict; |
5185 | 0 | const xmlChar *copy; |
5186 | 0 | const xmlChar *oldName; |
5187 | |
|
5188 | 0 | if (cur == NULL) return; |
5189 | 0 | if (name == NULL) return; |
5190 | 0 | switch(cur->type) { |
5191 | 0 | case XML_ELEMENT_NODE: |
5192 | 0 | case XML_ATTRIBUTE_NODE: |
5193 | 0 | case XML_PI_NODE: |
5194 | 0 | case XML_ENTITY_REF_NODE: |
5195 | 0 | break; |
5196 | 0 | default: |
5197 | 0 | return; |
5198 | 0 | } |
5199 | | |
5200 | 0 | doc = cur->doc; |
5201 | 0 | if (doc != NULL) |
5202 | 0 | dict = doc->dict; |
5203 | 0 | else |
5204 | 0 | dict = NULL; |
5205 | |
|
5206 | 0 | if (dict != NULL) |
5207 | 0 | copy = xmlDictLookup(dict, name, -1); |
5208 | 0 | else |
5209 | 0 | copy = xmlStrdup(name); |
5210 | 0 | if (copy == NULL) |
5211 | 0 | return; |
5212 | | |
5213 | 0 | oldName = cur->name; |
5214 | 0 | cur->name = copy; |
5215 | 0 | if ((oldName != NULL) && |
5216 | 0 | ((dict == NULL) || (!xmlDictOwns(dict, oldName)))) |
5217 | 0 | xmlFree((xmlChar *) oldName); |
5218 | 0 | } |
5219 | | |
5220 | | /** |
5221 | | * xmlNodeSetBase: |
5222 | | * @cur: the node being changed |
5223 | | * @uri: the new base URI |
5224 | | * |
5225 | | * Set (or reset) the base URI of a node, i.e. the value of the |
5226 | | * xml:base attribute. |
5227 | | * |
5228 | | * Returns 0 on success, -1 on error. |
5229 | | */ |
5230 | | int |
5231 | 0 | xmlNodeSetBase(xmlNodePtr cur, const xmlChar* uri) { |
5232 | 0 | xmlNsPtr ns; |
5233 | 0 | xmlChar* fixed; |
5234 | |
|
5235 | 0 | if (cur == NULL) |
5236 | 0 | return(-1); |
5237 | 0 | switch(cur->type) { |
5238 | 0 | case XML_ELEMENT_NODE: |
5239 | 0 | case XML_ATTRIBUTE_NODE: |
5240 | 0 | break; |
5241 | 0 | case XML_DOCUMENT_NODE: |
5242 | 0 | case XML_HTML_DOCUMENT_NODE: { |
5243 | 0 | xmlDocPtr doc = (xmlDocPtr) cur; |
5244 | |
|
5245 | 0 | if (doc->URL != NULL) |
5246 | 0 | xmlFree((xmlChar *) doc->URL); |
5247 | 0 | if (uri == NULL) { |
5248 | 0 | doc->URL = NULL; |
5249 | 0 | } else { |
5250 | 0 | doc->URL = xmlPathToURI(uri); |
5251 | 0 | if (doc->URL == NULL) |
5252 | 0 | return(-1); |
5253 | 0 | } |
5254 | 0 | return(0); |
5255 | 0 | } |
5256 | 0 | default: |
5257 | 0 | return(-1); |
5258 | 0 | } |
5259 | | |
5260 | 0 | xmlSearchNsByHrefSafe(cur, XML_XML_NAMESPACE, &ns); |
5261 | 0 | if (ns == NULL) |
5262 | 0 | return(-1); |
5263 | 0 | fixed = xmlPathToURI(uri); |
5264 | 0 | if (fixed == NULL) |
5265 | 0 | return(-1); |
5266 | 0 | if (xmlSetNsProp(cur, ns, BAD_CAST "base", fixed) == NULL) { |
5267 | 0 | xmlFree(fixed); |
5268 | 0 | return(-1); |
5269 | 0 | } |
5270 | 0 | xmlFree(fixed); |
5271 | |
|
5272 | 0 | return(0); |
5273 | 0 | } |
5274 | | |
5275 | | /** |
5276 | | * xmlNodeGetBaseSafe: |
5277 | | * @doc: the document the node pertains to |
5278 | | * @cur: the node being checked |
5279 | | * @baseOut: pointer to base |
5280 | | * |
5281 | | * Searches for the BASE URL. The code should work on both XML |
5282 | | * and HTML document even if base mechanisms are completely different. |
5283 | | * It returns the base as defined in RFC 2396 sections |
5284 | | * 5.1.1. Base URI within Document Content |
5285 | | * and |
5286 | | * 5.1.2. Base URI from the Encapsulating Entity |
5287 | | * However it does not return the document base (5.1.3), use |
5288 | | * doc->URL in this case |
5289 | | * |
5290 | | * Available since 2.13.0. |
5291 | | * |
5292 | | * Return 0 in case of success, 1 if a URI or argument is invalid, -1 if a |
5293 | | * memory allocation failed. |
5294 | | */ |
5295 | | int |
5296 | 0 | xmlNodeGetBaseSafe(const xmlDoc *doc, const xmlNode *cur, xmlChar **baseOut) { |
5297 | 0 | xmlChar *ret = NULL; |
5298 | 0 | xmlChar *base, *newbase; |
5299 | 0 | int res; |
5300 | |
|
5301 | 0 | if (baseOut == NULL) |
5302 | 0 | return(1); |
5303 | 0 | *baseOut = NULL; |
5304 | 0 | if ((cur == NULL) && (doc == NULL)) |
5305 | 0 | return(1); |
5306 | 0 | if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) |
5307 | 0 | return(1); |
5308 | 0 | if (doc == NULL) |
5309 | 0 | doc = cur->doc; |
5310 | |
|
5311 | 0 | if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { |
5312 | 0 | cur = doc->children; |
5313 | 0 | while ((cur != NULL) && (cur->name != NULL)) { |
5314 | 0 | if (cur->type != XML_ELEMENT_NODE) { |
5315 | 0 | cur = cur->next; |
5316 | 0 | continue; |
5317 | 0 | } |
5318 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { |
5319 | 0 | cur = cur->children; |
5320 | 0 | continue; |
5321 | 0 | } |
5322 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { |
5323 | 0 | cur = cur->children; |
5324 | 0 | continue; |
5325 | 0 | } |
5326 | 0 | if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { |
5327 | 0 | if (xmlNodeGetAttrValue(cur, BAD_CAST "href", NULL, &ret) < 0) |
5328 | 0 | return(-1); |
5329 | 0 | if (ret == NULL) |
5330 | 0 | return(1); |
5331 | 0 | goto found; |
5332 | 0 | } |
5333 | 0 | cur = cur->next; |
5334 | 0 | } |
5335 | 0 | return(0); |
5336 | 0 | } |
5337 | | |
5338 | 0 | while (cur != NULL) { |
5339 | 0 | if (cur->type == XML_ENTITY_DECL) { |
5340 | 0 | xmlEntityPtr ent = (xmlEntityPtr) cur; |
5341 | |
|
5342 | 0 | if (ent->URI == NULL) |
5343 | 0 | break; |
5344 | 0 | xmlFree(ret); |
5345 | 0 | ret = xmlStrdup(ent->URI); |
5346 | 0 | if (ret == NULL) |
5347 | 0 | return(-1); |
5348 | 0 | goto found; |
5349 | 0 | } |
5350 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
5351 | 0 | if (xmlNodeGetAttrValue(cur, BAD_CAST "base", XML_XML_NAMESPACE, |
5352 | 0 | &base) < 0) { |
5353 | 0 | xmlFree(ret); |
5354 | 0 | return(-1); |
5355 | 0 | } |
5356 | 0 | if (base != NULL) { |
5357 | 0 | if (ret != NULL) { |
5358 | 0 | res = xmlBuildURISafe(ret, base, &newbase); |
5359 | 0 | xmlFree(ret); |
5360 | 0 | xmlFree(base); |
5361 | 0 | if (res != 0) |
5362 | 0 | return(res); |
5363 | 0 | ret = newbase; |
5364 | 0 | } else { |
5365 | 0 | ret = base; |
5366 | 0 | } |
5367 | 0 | if ((!xmlStrncmp(ret, BAD_CAST "http://", 7)) || |
5368 | 0 | (!xmlStrncmp(ret, BAD_CAST "ftp://", 6)) || |
5369 | 0 | (!xmlStrncmp(ret, BAD_CAST "urn:", 4))) |
5370 | 0 | goto found; |
5371 | 0 | } |
5372 | 0 | } |
5373 | 0 | cur = cur->parent; |
5374 | 0 | } |
5375 | | |
5376 | 0 | if ((doc != NULL) && (doc->URL != NULL)) { |
5377 | 0 | if (ret == NULL) { |
5378 | 0 | ret = xmlStrdup(doc->URL); |
5379 | 0 | if (ret == NULL) |
5380 | 0 | return(-1); |
5381 | 0 | } else { |
5382 | 0 | res = xmlBuildURISafe(ret, doc->URL, &newbase); |
5383 | 0 | xmlFree(ret); |
5384 | 0 | if (res != 0) |
5385 | 0 | return(res); |
5386 | 0 | ret = newbase; |
5387 | 0 | } |
5388 | 0 | } |
5389 | | |
5390 | 0 | found: |
5391 | 0 | *baseOut = ret; |
5392 | 0 | return(0); |
5393 | 0 | } |
5394 | | |
5395 | | /** |
5396 | | * xmlNodeGetBase: |
5397 | | * @doc: the document the node pertains to |
5398 | | * @cur: the node being checked |
5399 | | * |
5400 | | * See xmlNodeGetBaseSafe. This function doesn't allow to distinguish |
5401 | | * memory allocation failures from a non-existing base. |
5402 | | * |
5403 | | * Returns a pointer to the base URL, or NULL if not found |
5404 | | * It's up to the caller to free the memory with xmlFree(). |
5405 | | */ |
5406 | | xmlChar * |
5407 | 0 | xmlNodeGetBase(const xmlDoc *doc, const xmlNode *cur) { |
5408 | 0 | xmlChar *base; |
5409 | |
|
5410 | 0 | xmlNodeGetBaseSafe(doc, cur, &base); |
5411 | 0 | return(base); |
5412 | 0 | } |
5413 | | |
5414 | | /** |
5415 | | * xmlNodeBufGetContent: |
5416 | | * @buffer: a buffer |
5417 | | * @cur: the node being read |
5418 | | * |
5419 | | * Read the value of a node @cur, this can be either the text carried |
5420 | | * directly by this node if it's a TEXT node or the aggregate string |
5421 | | * of the values carried by this node child's (TEXT and ENTITY_REF). |
5422 | | * Entity references are substituted. |
5423 | | * Fills up the buffer @buffer with this value |
5424 | | * |
5425 | | * Returns 0 in case of success and -1 in case of error. |
5426 | | */ |
5427 | | int |
5428 | | xmlNodeBufGetContent(xmlBufferPtr buffer, const xmlNode *cur) |
5429 | 0 | { |
5430 | 0 | xmlBufPtr buf; |
5431 | 0 | int ret1, ret2; |
5432 | |
|
5433 | 0 | if ((cur == NULL) || (buffer == NULL)) return(-1); |
5434 | 0 | buf = xmlBufFromBuffer(buffer); |
5435 | 0 | ret1 = xmlBufGetNodeContent(buf, cur); |
5436 | 0 | ret2 = xmlBufBackToBuffer(buf, buffer); |
5437 | 0 | if ((ret1 < 0) || (ret2 < 0)) |
5438 | 0 | return(-1); |
5439 | 0 | return(0); |
5440 | 0 | } |
5441 | | |
5442 | | static void |
5443 | 0 | xmlBufGetEntityRefContent(xmlBufPtr buf, const xmlNode *ref) { |
5444 | 0 | xmlEntityPtr ent; |
5445 | |
|
5446 | 0 | if (ref->children != NULL) { |
5447 | 0 | ent = (xmlEntityPtr) ref->children; |
5448 | 0 | } else { |
5449 | | /* lookup entity declaration */ |
5450 | 0 | ent = xmlGetDocEntity(ref->doc, ref->name); |
5451 | 0 | if (ent == NULL) |
5452 | 0 | return; |
5453 | 0 | } |
5454 | | |
5455 | | /* |
5456 | | * The parser should always expand predefined entities but it's |
5457 | | * possible to create references to predefined entities using |
5458 | | * the tree API. |
5459 | | */ |
5460 | 0 | if (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY) { |
5461 | 0 | xmlBufCat(buf, ent->content); |
5462 | 0 | return; |
5463 | 0 | } |
5464 | | |
5465 | 0 | if (ent->flags & XML_ENT_EXPANDING) |
5466 | 0 | return; |
5467 | | |
5468 | 0 | ent->flags |= XML_ENT_EXPANDING; |
5469 | 0 | xmlBufGetChildContent(buf, (xmlNodePtr) ent); |
5470 | 0 | ent->flags &= ~XML_ENT_EXPANDING; |
5471 | 0 | } |
5472 | | |
5473 | | static void |
5474 | 0 | xmlBufGetChildContent(xmlBufPtr buf, const xmlNode *tree) { |
5475 | 0 | const xmlNode *cur = tree->children; |
5476 | |
|
5477 | 0 | while (cur != NULL) { |
5478 | 0 | switch (cur->type) { |
5479 | 0 | case XML_TEXT_NODE: |
5480 | 0 | case XML_CDATA_SECTION_NODE: |
5481 | 0 | xmlBufCat(buf, cur->content); |
5482 | 0 | break; |
5483 | | |
5484 | 0 | case XML_ENTITY_REF_NODE: |
5485 | 0 | xmlBufGetEntityRefContent(buf, cur); |
5486 | 0 | break; |
5487 | | |
5488 | 0 | default: |
5489 | 0 | if (cur->children != NULL) { |
5490 | 0 | cur = cur->children; |
5491 | 0 | continue; |
5492 | 0 | } |
5493 | 0 | break; |
5494 | 0 | } |
5495 | | |
5496 | 0 | while (cur->next == NULL) { |
5497 | 0 | cur = cur->parent; |
5498 | 0 | if (cur == tree) |
5499 | 0 | return; |
5500 | 0 | } |
5501 | 0 | cur = cur->next; |
5502 | 0 | } |
5503 | 0 | } |
5504 | | |
5505 | | /** |
5506 | | * xmlBufGetNodeContent: |
5507 | | * @buf: a buffer xmlBufPtr |
5508 | | * @cur: the node being read |
5509 | | * |
5510 | | * Read the value of a node @cur, this can be either the text carried |
5511 | | * directly by this node if it's a TEXT node or the aggregate string |
5512 | | * of the values carried by this node child's (TEXT and ENTITY_REF). |
5513 | | * Entity references are substituted. |
5514 | | * Fills up the buffer @buf with this value |
5515 | | * |
5516 | | * Returns 0 in case of success and -1 in case of error. |
5517 | | */ |
5518 | | int |
5519 | | xmlBufGetNodeContent(xmlBufPtr buf, const xmlNode *cur) |
5520 | 0 | { |
5521 | 0 | if ((cur == NULL) || (buf == NULL)) |
5522 | 0 | return(-1); |
5523 | | |
5524 | 0 | switch (cur->type) { |
5525 | 0 | case XML_DOCUMENT_NODE: |
5526 | 0 | case XML_HTML_DOCUMENT_NODE: |
5527 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5528 | 0 | case XML_ELEMENT_NODE: |
5529 | 0 | case XML_ATTRIBUTE_NODE: |
5530 | 0 | case XML_ENTITY_DECL: |
5531 | 0 | xmlBufGetChildContent(buf, cur); |
5532 | 0 | break; |
5533 | | |
5534 | 0 | case XML_CDATA_SECTION_NODE: |
5535 | 0 | case XML_TEXT_NODE: |
5536 | 0 | case XML_COMMENT_NODE: |
5537 | 0 | case XML_PI_NODE: |
5538 | 0 | xmlBufCat(buf, cur->content); |
5539 | 0 | break; |
5540 | | |
5541 | 0 | case XML_ENTITY_REF_NODE: |
5542 | 0 | xmlBufGetEntityRefContent(buf, cur); |
5543 | 0 | break; |
5544 | | |
5545 | 0 | case XML_NAMESPACE_DECL: |
5546 | 0 | xmlBufCat(buf, ((xmlNsPtr) cur)->href); |
5547 | 0 | break; |
5548 | | |
5549 | 0 | default: |
5550 | 0 | break; |
5551 | 0 | } |
5552 | | |
5553 | 0 | return(0); |
5554 | 0 | } |
5555 | | |
5556 | | /** |
5557 | | * xmlNodeGetContent: |
5558 | | * @cur: the node being read |
5559 | | * |
5560 | | * Read the value of a node, this can be either the text carried |
5561 | | * directly by this node if it's a TEXT node or the aggregate string |
5562 | | * of the values carried by this node child's (TEXT and ENTITY_REF). |
5563 | | * Entity references are substituted. |
5564 | | * Returns a new #xmlChar * or NULL if no content is available. |
5565 | | * It's up to the caller to free the memory with xmlFree(). |
5566 | | */ |
5567 | | xmlChar * |
5568 | | xmlNodeGetContent(const xmlNode *cur) |
5569 | 0 | { |
5570 | 0 | xmlBufPtr buf; |
5571 | 0 | xmlChar *ret; |
5572 | |
|
5573 | 0 | if (cur == NULL) |
5574 | 0 | return (NULL); |
5575 | | |
5576 | 0 | switch (cur->type) { |
5577 | 0 | case XML_DOCUMENT_NODE: |
5578 | 0 | case XML_HTML_DOCUMENT_NODE: |
5579 | 0 | case XML_ENTITY_REF_NODE: |
5580 | 0 | break; |
5581 | | |
5582 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5583 | 0 | case XML_ELEMENT_NODE: |
5584 | 0 | case XML_ATTRIBUTE_NODE: |
5585 | 0 | case XML_ENTITY_DECL: { |
5586 | 0 | xmlNodePtr children = cur->children; |
5587 | |
|
5588 | 0 | if (children == NULL) |
5589 | 0 | return(xmlStrdup(BAD_CAST "")); |
5590 | | |
5591 | | /* Optimization for single text children */ |
5592 | 0 | if (((children->type == XML_TEXT_NODE) || |
5593 | 0 | (children->type == XML_CDATA_SECTION_NODE)) && |
5594 | 0 | (children->next == NULL)) { |
5595 | 0 | if (children->content == NULL) |
5596 | 0 | return(xmlStrdup(BAD_CAST "")); |
5597 | 0 | return(xmlStrdup(children->content)); |
5598 | 0 | } |
5599 | | |
5600 | 0 | break; |
5601 | 0 | } |
5602 | | |
5603 | 0 | case XML_CDATA_SECTION_NODE: |
5604 | 0 | case XML_TEXT_NODE: |
5605 | 0 | case XML_COMMENT_NODE: |
5606 | 0 | case XML_PI_NODE: |
5607 | 0 | if (cur->content != NULL) |
5608 | 0 | return(xmlStrdup(cur->content)); |
5609 | 0 | else |
5610 | 0 | return(xmlStrdup(BAD_CAST "")); |
5611 | | |
5612 | 0 | case XML_NAMESPACE_DECL: |
5613 | 0 | return(xmlStrdup(((xmlNsPtr) cur)->href)); |
5614 | | |
5615 | 0 | default: |
5616 | 0 | return(NULL); |
5617 | 0 | } |
5618 | | |
5619 | 0 | buf = xmlBufCreate(50); |
5620 | 0 | if (buf == NULL) |
5621 | 0 | return (NULL); |
5622 | 0 | xmlBufGetNodeContent(buf, cur); |
5623 | 0 | ret = xmlBufDetach(buf); |
5624 | 0 | xmlBufFree(buf); |
5625 | |
|
5626 | 0 | return(ret); |
5627 | 0 | } |
5628 | | |
5629 | | static int |
5630 | 0 | xmlNodeSetContentInternal(xmlNodePtr cur, const xmlChar *content, int len) { |
5631 | 0 | if (cur == NULL) { |
5632 | 0 | return(1); |
5633 | 0 | } |
5634 | 0 | switch (cur->type) { |
5635 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5636 | 0 | case XML_ELEMENT_NODE: |
5637 | 0 | case XML_ATTRIBUTE_NODE: |
5638 | 0 | if (xmlNodeParseContent(cur, content, len) < 0) |
5639 | 0 | return(-1); |
5640 | 0 | break; |
5641 | | |
5642 | 0 | case XML_TEXT_NODE: |
5643 | 0 | case XML_CDATA_SECTION_NODE: |
5644 | 0 | case XML_PI_NODE: |
5645 | 0 | case XML_COMMENT_NODE: { |
5646 | 0 | xmlChar *copy = NULL; |
5647 | |
|
5648 | 0 | if (content != NULL) { |
5649 | 0 | if (len < 0) |
5650 | 0 | copy = xmlStrdup(content); |
5651 | 0 | else |
5652 | 0 | copy = xmlStrndup(content, len); |
5653 | 0 | if (copy == NULL) |
5654 | 0 | return(-1); |
5655 | 0 | } |
5656 | | |
5657 | 0 | xmlTextSetContent(cur, copy); |
5658 | 0 | break; |
5659 | 0 | } |
5660 | | |
5661 | 0 | default: |
5662 | 0 | break; |
5663 | 0 | } |
5664 | | |
5665 | 0 | return(0); |
5666 | 0 | } |
5667 | | |
5668 | | /** |
5669 | | * xmlNodeSetContent: |
5670 | | * @cur: the node being modified |
5671 | | * @content: the new value of the content |
5672 | | * |
5673 | | * Replace the text content of a node. |
5674 | | * |
5675 | | * Sets the raw text content of text, CDATA, comment or PI nodes. |
5676 | | * |
5677 | | * For element and attribute nodes, removes all children and |
5678 | | * replaces them by parsing @content which is expected to be a |
5679 | | * valid XML attribute value possibly containing character and |
5680 | | * entity references. Syntax errors and references to undeclared |
5681 | | * entities are ignored silently. Unfortunately, there isn't an |
5682 | | * API to pass raw content directly. An inefficient work-around |
5683 | | * is to escape the content with xmlEncodeSpecialChars before |
5684 | | * passing it. A better trick is clearing the old content |
5685 | | * with xmlNodeSetContent(node, NULL) first and then calling |
5686 | | * xmlNodeAddContent(node, content). Unlike this function, |
5687 | | * xmlNodeAddContent accepts raw text. |
5688 | | * |
5689 | | * Returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5690 | | */ |
5691 | | int |
5692 | 0 | xmlNodeSetContent(xmlNodePtr cur, const xmlChar *content) { |
5693 | 0 | return(xmlNodeSetContentInternal(cur, content, -1)); |
5694 | 0 | } |
5695 | | |
5696 | | /** |
5697 | | * xmlNodeSetContentLen: |
5698 | | * @cur: the node being modified |
5699 | | * @content: the new value of the content |
5700 | | * @len: the size of @content |
5701 | | * |
5702 | | * See xmlNodeSetContent. |
5703 | | * |
5704 | | * Returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5705 | | */ |
5706 | | int |
5707 | 0 | xmlNodeSetContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
5708 | 0 | return(xmlNodeSetContentInternal(cur, content, len)); |
5709 | 0 | } |
5710 | | |
5711 | | /** |
5712 | | * xmlNodeAddContentLen: |
5713 | | * @cur: the node being modified |
5714 | | * @content: extra content |
5715 | | * @len: the size of @content |
5716 | | * |
5717 | | * Append the extra substring to the node content. |
5718 | | * NOTE: In contrast to xmlNodeSetContentLen(), @content is supposed to be |
5719 | | * raw text, so unescaped XML special chars are allowed, entity |
5720 | | * references are not supported. |
5721 | | * |
5722 | | * Returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5723 | | */ |
5724 | | int |
5725 | 0 | xmlNodeAddContentLen(xmlNodePtr cur, const xmlChar *content, int len) { |
5726 | 0 | if (cur == NULL) |
5727 | 0 | return(1); |
5728 | 0 | if ((content == NULL) || (len <= 0)) |
5729 | 0 | return(0); |
5730 | | |
5731 | 0 | switch (cur->type) { |
5732 | 0 | case XML_DOCUMENT_FRAG_NODE: |
5733 | 0 | case XML_ELEMENT_NODE: { |
5734 | 0 | xmlNodePtr newNode, tmp; |
5735 | |
|
5736 | 0 | newNode = xmlNewDocTextLen(cur->doc, content, len); |
5737 | 0 | if (newNode == NULL) |
5738 | 0 | return(-1); |
5739 | 0 | tmp = xmlAddChild(cur, newNode); |
5740 | 0 | if (tmp == NULL) { |
5741 | 0 | xmlFreeNode(newNode); |
5742 | 0 | return(-1); |
5743 | 0 | } |
5744 | 0 | break; |
5745 | 0 | } |
5746 | 0 | case XML_ATTRIBUTE_NODE: |
5747 | 0 | break; |
5748 | 0 | case XML_TEXT_NODE: |
5749 | 0 | case XML_CDATA_SECTION_NODE: |
5750 | 0 | case XML_PI_NODE: |
5751 | 0 | case XML_COMMENT_NODE: |
5752 | 0 | return(xmlTextAddContent(cur, content, len)); |
5753 | 0 | default: |
5754 | 0 | break; |
5755 | 0 | } |
5756 | | |
5757 | 0 | return(0); |
5758 | 0 | } |
5759 | | |
5760 | | /** |
5761 | | * xmlNodeAddContent: |
5762 | | * @cur: the node being modified |
5763 | | * @content: extra content |
5764 | | * |
5765 | | * Append the extra substring to the node content. |
5766 | | * NOTE: In contrast to xmlNodeSetContent(), @content is supposed to be |
5767 | | * raw text, so unescaped XML special chars are allowed, entity |
5768 | | * references are not supported. |
5769 | | * |
5770 | | * Returns 0 on success, 1 on error, -1 if a memory allocation failed. |
5771 | | */ |
5772 | | int |
5773 | 0 | xmlNodeAddContent(xmlNodePtr cur, const xmlChar *content) { |
5774 | 0 | return(xmlNodeAddContentLen(cur, content, xmlStrlen(content))); |
5775 | 0 | } |
5776 | | |
5777 | | /** |
5778 | | * xmlTextMerge: |
5779 | | * @first: the first text node |
5780 | | * @second: the second text node being merged |
5781 | | * |
5782 | | * Merge the second text node into the first. The second node is |
5783 | | * unlinked and freed. |
5784 | | * |
5785 | | * Returns the first text node augmented or NULL in case of error. |
5786 | | */ |
5787 | | xmlNodePtr |
5788 | 0 | xmlTextMerge(xmlNodePtr first, xmlNodePtr second) { |
5789 | 0 | if ((first == NULL) || (first->type != XML_TEXT_NODE) || |
5790 | 0 | (second == NULL) || (second->type != XML_TEXT_NODE) || |
5791 | 0 | (first == second) || |
5792 | 0 | (first->name != second->name)) |
5793 | 0 | return(NULL); |
5794 | | |
5795 | 0 | if (xmlTextAddContent(first, second->content, -1) < 0) |
5796 | 0 | return(NULL); |
5797 | | |
5798 | 0 | xmlUnlinkNodeInternal(second); |
5799 | 0 | xmlFreeNode(second); |
5800 | 0 | return(first); |
5801 | 0 | } |
5802 | | |
5803 | | /** |
5804 | | * xmlGetNsListSafe: |
5805 | | * @doc: the document |
5806 | | * @node: the current node |
5807 | | * @out: the returned namespace array |
5808 | | * |
5809 | | * Find all in-scope namespaces of a node. @out returns a NULL |
5810 | | * terminated array of namespace pointers that must be freed by |
5811 | | * the caller. |
5812 | | * |
5813 | | * Available since 2.13.0. |
5814 | | * |
5815 | | * Returns 0 on success, 1 if no namespaces were found, -1 if a |
5816 | | * memory allocation failed. |
5817 | | */ |
5818 | | int |
5819 | | xmlGetNsListSafe(const xmlDoc *doc ATTRIBUTE_UNUSED, const xmlNode *node, |
5820 | | xmlNsPtr **out) |
5821 | 0 | { |
5822 | 0 | xmlNsPtr cur; |
5823 | 0 | xmlNsPtr *namespaces = NULL; |
5824 | 0 | int nbns = 0; |
5825 | 0 | int maxns = 0; |
5826 | 0 | int i; |
5827 | |
|
5828 | 0 | if (out == NULL) |
5829 | 0 | return(1); |
5830 | 0 | *out = NULL; |
5831 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
5832 | 0 | return(1); |
5833 | | |
5834 | 0 | while (node != NULL) { |
5835 | 0 | if (node->type == XML_ELEMENT_NODE) { |
5836 | 0 | cur = node->nsDef; |
5837 | 0 | while (cur != NULL) { |
5838 | 0 | for (i = 0; i < nbns; i++) { |
5839 | 0 | if ((cur->prefix == namespaces[i]->prefix) || |
5840 | 0 | (xmlStrEqual(cur->prefix, namespaces[i]->prefix))) |
5841 | 0 | break; |
5842 | 0 | } |
5843 | 0 | if (i >= nbns) { |
5844 | 0 | if (nbns >= maxns) { |
5845 | 0 | xmlNsPtr *tmp; |
5846 | |
|
5847 | 0 | maxns = maxns ? maxns * 2 : 10; |
5848 | 0 | tmp = (xmlNsPtr *) xmlRealloc(namespaces, |
5849 | 0 | (maxns + 1) * |
5850 | 0 | sizeof(xmlNsPtr)); |
5851 | 0 | if (tmp == NULL) { |
5852 | 0 | xmlFree(namespaces); |
5853 | 0 | return(-1); |
5854 | 0 | } |
5855 | 0 | namespaces = tmp; |
5856 | 0 | } |
5857 | 0 | namespaces[nbns++] = cur; |
5858 | 0 | namespaces[nbns] = NULL; |
5859 | 0 | } |
5860 | | |
5861 | 0 | cur = cur->next; |
5862 | 0 | } |
5863 | 0 | } |
5864 | 0 | node = node->parent; |
5865 | 0 | } |
5866 | | |
5867 | 0 | *out = namespaces; |
5868 | 0 | return((namespaces == NULL) ? 1 : 0); |
5869 | 0 | } |
5870 | | |
5871 | | /** |
5872 | | * xmlGetNsList: |
5873 | | * @doc: the document |
5874 | | * @node: the current node |
5875 | | * |
5876 | | * Find all in-scope namespaces of a node. |
5877 | | * |
5878 | | * Use xmlGetNsListSafe for better error reporting. |
5879 | | * |
5880 | | * Returns a NULL terminated array of namespace pointers that must |
5881 | | * be freed by the caller or NULL if no namespaces were found or |
5882 | | * a memory allocation failed. |
5883 | | */ |
5884 | | xmlNsPtr * |
5885 | | xmlGetNsList(const xmlDoc *doc, const xmlNode *node) |
5886 | 0 | { |
5887 | 0 | xmlNsPtr *ret; |
5888 | |
|
5889 | 0 | xmlGetNsListSafe(doc, node, &ret); |
5890 | 0 | return(ret); |
5891 | 0 | } |
5892 | | |
5893 | | static xmlNsPtr |
5894 | 774 | xmlNewXmlNs(void) { |
5895 | 774 | xmlNsPtr ns; |
5896 | | |
5897 | 774 | ns = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
5898 | 774 | if (ns == NULL) |
5899 | 0 | return(NULL); |
5900 | 774 | memset(ns, 0, sizeof(xmlNs)); |
5901 | 774 | ns->type = XML_LOCAL_NAMESPACE; |
5902 | 774 | ns->href = xmlStrdup(XML_XML_NAMESPACE); |
5903 | 774 | if (ns->href == NULL) { |
5904 | 0 | xmlFreeNs(ns); |
5905 | 0 | return(NULL); |
5906 | 0 | } |
5907 | 774 | ns->prefix = xmlStrdup(BAD_CAST "xml"); |
5908 | 774 | if (ns->prefix == NULL) { |
5909 | 0 | xmlFreeNs(ns); |
5910 | 0 | return(NULL); |
5911 | 0 | } |
5912 | | |
5913 | 774 | return(ns); |
5914 | 774 | } |
5915 | | |
5916 | | /* |
5917 | | * xmlTreeEnsureXMLDecl: |
5918 | | * @doc: the doc |
5919 | | * |
5920 | | * Ensures that there is an XML namespace declaration on the doc. |
5921 | | * |
5922 | | * Returns the XML ns-struct or NULL if a memory allocation failed. |
5923 | | */ |
5924 | | static xmlNsPtr |
5925 | | xmlTreeEnsureXMLDecl(xmlDocPtr doc) |
5926 | 8.13k | { |
5927 | 8.13k | xmlNsPtr ns; |
5928 | | |
5929 | 8.13k | ns = doc->oldNs; |
5930 | 8.13k | if (ns != NULL) |
5931 | 7.35k | return (ns); |
5932 | | |
5933 | 774 | ns = xmlNewXmlNs(); |
5934 | 774 | doc->oldNs = ns; |
5935 | | |
5936 | 774 | return(ns); |
5937 | 8.13k | } |
5938 | | |
5939 | | /** |
5940 | | * xmlSearchNsSafe: |
5941 | | * @node: a node |
5942 | | * @prefix: a namespace prefix |
5943 | | * @out: pointer to resulting namespace |
5944 | | * |
5945 | | * Search a namespace with @prefix in scope of @node. |
5946 | | * |
5947 | | * Returns 0 on success, -1 if a memory allocation failed, 1 on |
5948 | | * other errors. |
5949 | | */ |
5950 | | int |
5951 | | xmlSearchNsSafe(xmlNodePtr node, const xmlChar *prefix, |
5952 | 8.13k | xmlNsPtr *out) { |
5953 | 8.13k | xmlNsPtr cur; |
5954 | 8.13k | xmlDocPtr doc; |
5955 | 8.13k | xmlNodePtr orig = node; |
5956 | 8.13k | xmlNodePtr parent; |
5957 | | |
5958 | 8.13k | if (out == NULL) |
5959 | 0 | return(1); |
5960 | 8.13k | *out = NULL; |
5961 | 8.13k | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
5962 | 0 | return(1); |
5963 | | |
5964 | 8.13k | doc = node->doc; |
5965 | | |
5966 | 8.13k | if ((doc != NULL) && (IS_STR_XML(prefix))) { |
5967 | 8.13k | cur = xmlTreeEnsureXMLDecl(doc); |
5968 | 8.13k | if (cur == NULL) |
5969 | 0 | return(-1); |
5970 | 8.13k | *out = cur; |
5971 | 8.13k | return(0); |
5972 | 8.13k | } |
5973 | | |
5974 | 0 | while (node->type != XML_ELEMENT_NODE) { |
5975 | 0 | node = node->parent; |
5976 | 0 | if (node == NULL) |
5977 | 0 | return(0); |
5978 | 0 | } |
5979 | | |
5980 | 0 | parent = node; |
5981 | |
|
5982 | 0 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
5983 | 0 | cur = node->nsDef; |
5984 | 0 | while (cur != NULL) { |
5985 | 0 | if ((xmlStrEqual(cur->prefix, prefix)) && |
5986 | 0 | (cur->href != NULL)) { |
5987 | 0 | *out = cur; |
5988 | 0 | return(0); |
5989 | 0 | } |
5990 | 0 | cur = cur->next; |
5991 | 0 | } |
5992 | 0 | if (orig != node) { |
5993 | 0 | cur = node->ns; |
5994 | 0 | if ((cur != NULL) && |
5995 | 0 | (xmlStrEqual(cur->prefix, prefix)) && |
5996 | 0 | (cur->href != NULL)) { |
5997 | 0 | *out = cur; |
5998 | 0 | return(0); |
5999 | 0 | } |
6000 | 0 | } |
6001 | | |
6002 | 0 | node = node->parent; |
6003 | 0 | } |
6004 | | |
6005 | | /* |
6006 | | * The XML-1.0 namespace is normally held on the document |
6007 | | * element. In this case exceptionally create it on the |
6008 | | * node element. |
6009 | | */ |
6010 | 0 | if ((doc == NULL) && (IS_STR_XML(prefix))) { |
6011 | 0 | cur = xmlNewXmlNs(); |
6012 | 0 | if (cur == NULL) |
6013 | 0 | return(-1); |
6014 | 0 | cur->next = parent->nsDef; |
6015 | 0 | parent->nsDef = cur; |
6016 | 0 | *out = cur; |
6017 | 0 | } |
6018 | | |
6019 | 0 | return(0); |
6020 | 0 | } |
6021 | | |
6022 | | /** |
6023 | | * xmlSearchNs: |
6024 | | * @doc: the document |
6025 | | * @node: the current node |
6026 | | * @nameSpace: the namespace prefix |
6027 | | * |
6028 | | * Search a Ns registered under a given name space for a document. |
6029 | | * recurse on the parents until it finds the defined namespace |
6030 | | * or return NULL otherwise. |
6031 | | * @nameSpace can be NULL, this is a search for the default namespace. |
6032 | | * We don't allow to cross entities boundaries. If you don't declare |
6033 | | * the namespace within those you will be in troubles !!! A warning |
6034 | | * is generated to cover this case. |
6035 | | * |
6036 | | * Returns the namespace pointer or NULL if no namespace was found or |
6037 | | * a memory allocation failed. Allocations can only fail if the "xml" |
6038 | | * namespace is queried. |
6039 | | */ |
6040 | | xmlNsPtr |
6041 | | xmlSearchNs(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
6042 | 0 | const xmlChar *nameSpace) { |
6043 | 0 | xmlNsPtr cur; |
6044 | |
|
6045 | 0 | xmlSearchNsSafe(node, nameSpace, &cur); |
6046 | 0 | return(cur); |
6047 | 0 | } |
6048 | | |
6049 | | /** |
6050 | | * xmlNsInScope: |
6051 | | * @doc: the document |
6052 | | * @node: the current node |
6053 | | * @ancestor: the ancestor carrying the namespace |
6054 | | * @prefix: the namespace prefix |
6055 | | * |
6056 | | * Verify that the given namespace held on @ancestor is still in scope |
6057 | | * on node. |
6058 | | * |
6059 | | * Returns 1 if true, 0 if false and -1 in case of error. |
6060 | | */ |
6061 | | static int |
6062 | | xmlNsInScope(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
6063 | | xmlNodePtr ancestor, const xmlChar * prefix) |
6064 | 0 | { |
6065 | 0 | xmlNsPtr tst; |
6066 | |
|
6067 | 0 | while ((node != NULL) && (node != ancestor)) { |
6068 | 0 | if ((node->type == XML_ENTITY_REF_NODE) || |
6069 | 0 | (node->type == XML_ENTITY_DECL)) |
6070 | 0 | return (-1); |
6071 | 0 | if (node->type == XML_ELEMENT_NODE) { |
6072 | 0 | tst = node->nsDef; |
6073 | 0 | while (tst != NULL) { |
6074 | 0 | if ((tst->prefix == NULL) |
6075 | 0 | && (prefix == NULL)) |
6076 | 0 | return (0); |
6077 | 0 | if ((tst->prefix != NULL) |
6078 | 0 | && (prefix != NULL) |
6079 | 0 | && (xmlStrEqual(tst->prefix, prefix))) |
6080 | 0 | return (0); |
6081 | 0 | tst = tst->next; |
6082 | 0 | } |
6083 | 0 | } |
6084 | 0 | node = node->parent; |
6085 | 0 | } |
6086 | 0 | if (node != ancestor) |
6087 | 0 | return (-1); |
6088 | 0 | return (1); |
6089 | 0 | } |
6090 | | |
6091 | | /** |
6092 | | * xmlSearchNsByHrefSafe: |
6093 | | * @node: a node |
6094 | | * @href: a namespace URI |
6095 | | * @out: pointer to resulting namespace |
6096 | | * |
6097 | | * Search a namespace matching @URI in scope of @node. |
6098 | | * |
6099 | | * Returns 0 on success, -1 if a memory allocation failed, 1 on |
6100 | | * other errors. |
6101 | | */ |
6102 | | int |
6103 | | xmlSearchNsByHrefSafe(xmlNodePtr node, const xmlChar *href, |
6104 | 0 | xmlNsPtr *out) { |
6105 | 0 | xmlNsPtr cur; |
6106 | 0 | xmlDocPtr doc; |
6107 | 0 | xmlNodePtr orig = node; |
6108 | 0 | xmlNodePtr parent; |
6109 | 0 | int is_attr; |
6110 | |
|
6111 | 0 | if (out == NULL) |
6112 | 0 | return(1); |
6113 | 0 | *out = NULL; |
6114 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
6115 | 0 | return(1); |
6116 | | |
6117 | 0 | doc = node->doc; |
6118 | |
|
6119 | 0 | if ((doc != NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) { |
6120 | 0 | cur = xmlTreeEnsureXMLDecl(doc); |
6121 | 0 | if (cur == NULL) |
6122 | 0 | return(-1); |
6123 | 0 | *out = cur; |
6124 | 0 | return(0); |
6125 | 0 | } |
6126 | | |
6127 | 0 | is_attr = (node->type == XML_ATTRIBUTE_NODE); |
6128 | |
|
6129 | 0 | while (node->type != XML_ELEMENT_NODE) { |
6130 | 0 | node = node->parent; |
6131 | 0 | if (node == NULL) |
6132 | 0 | return(0); |
6133 | 0 | } |
6134 | | |
6135 | 0 | parent = node; |
6136 | |
|
6137 | 0 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
6138 | 0 | cur = node->nsDef; |
6139 | 0 | while (cur != NULL) { |
6140 | 0 | if (xmlStrEqual(cur->href, href)) { |
6141 | 0 | if (((!is_attr) || (cur->prefix != NULL)) && |
6142 | 0 | (xmlNsInScope(doc, orig, node, cur->prefix) == 1)) { |
6143 | 0 | *out = cur; |
6144 | 0 | return(0); |
6145 | 0 | } |
6146 | 0 | } |
6147 | 0 | cur = cur->next; |
6148 | 0 | } |
6149 | 0 | if (orig != node) { |
6150 | 0 | cur = node->ns; |
6151 | 0 | if (cur != NULL) { |
6152 | 0 | if (xmlStrEqual(cur->href, href)) { |
6153 | 0 | if (((!is_attr) || (cur->prefix != NULL)) && |
6154 | 0 | (xmlNsInScope(doc, orig, node, |
6155 | 0 | cur->prefix) == 1)) { |
6156 | 0 | *out = cur; |
6157 | 0 | return(0); |
6158 | 0 | } |
6159 | 0 | } |
6160 | 0 | } |
6161 | 0 | } |
6162 | | |
6163 | 0 | node = node->parent; |
6164 | 0 | } |
6165 | | |
6166 | | /* |
6167 | | * The XML-1.0 namespace is normally held on the document |
6168 | | * element. In this case exceptionally create it on the |
6169 | | * node element. |
6170 | | */ |
6171 | 0 | if ((doc == NULL) && (xmlStrEqual(href, XML_XML_NAMESPACE))) { |
6172 | 0 | cur = xmlNewXmlNs(); |
6173 | 0 | if (cur == NULL) |
6174 | 0 | return(-1); |
6175 | 0 | cur->next = parent->nsDef; |
6176 | 0 | parent->nsDef = cur; |
6177 | 0 | *out = cur; |
6178 | 0 | } |
6179 | | |
6180 | 0 | return(0); |
6181 | 0 | } |
6182 | | |
6183 | | /** |
6184 | | * xmlSearchNsByHref: |
6185 | | * @doc: the document |
6186 | | * @node: the current node |
6187 | | * @href: the namespace value |
6188 | | * |
6189 | | * Search a Ns aliasing a given URI. Recurse on the parents until it finds |
6190 | | * the defined namespace or return NULL otherwise. |
6191 | | * |
6192 | | * Returns the namespace pointer or NULL if no namespace was found or |
6193 | | * a memory allocation failed. Allocations can only fail if the "xml" |
6194 | | * namespace is queried. |
6195 | | */ |
6196 | | xmlNsPtr |
6197 | | xmlSearchNsByHref(xmlDocPtr doc ATTRIBUTE_UNUSED, xmlNodePtr node, |
6198 | 0 | const xmlChar * href) { |
6199 | 0 | xmlNsPtr cur; |
6200 | |
|
6201 | 0 | xmlSearchNsByHrefSafe(node, href, &cur); |
6202 | 0 | return(cur); |
6203 | 0 | } |
6204 | | |
6205 | | /** |
6206 | | * xmlNewReconciledNs: |
6207 | | * @doc: the document |
6208 | | * @tree: a node expected to hold the new namespace |
6209 | | * @ns: the original namespace |
6210 | | * |
6211 | | * This function tries to locate a namespace definition in a tree |
6212 | | * ancestors, or create a new namespace definition node similar to |
6213 | | * @ns trying to reuse the same prefix. However if the given prefix is |
6214 | | * null (default namespace) or reused within the subtree defined by |
6215 | | * @tree or on one of its ancestors then a new prefix is generated. |
6216 | | * Returns the (new) namespace definition or NULL in case of error |
6217 | | */ |
6218 | | static xmlNsPtr |
6219 | 0 | xmlNewReconciledNs(xmlNodePtr tree, xmlNsPtr ns) { |
6220 | 0 | xmlNsPtr def; |
6221 | 0 | xmlChar prefix[50]; |
6222 | 0 | int counter = 1; |
6223 | 0 | int res; |
6224 | |
|
6225 | 0 | if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) { |
6226 | 0 | return(NULL); |
6227 | 0 | } |
6228 | 0 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) { |
6229 | 0 | return(NULL); |
6230 | 0 | } |
6231 | | /* |
6232 | | * Search an existing namespace definition inherited. |
6233 | | */ |
6234 | 0 | res = xmlSearchNsByHrefSafe(tree, ns->href, &def); |
6235 | 0 | if (res < 0) |
6236 | 0 | return(NULL); |
6237 | 0 | if (def != NULL) |
6238 | 0 | return(def); |
6239 | | |
6240 | | /* |
6241 | | * Find a close prefix which is not already in use. |
6242 | | * Let's strip namespace prefixes longer than 20 chars ! |
6243 | | */ |
6244 | 0 | if (ns->prefix == NULL) |
6245 | 0 | snprintf((char *) prefix, sizeof(prefix), "default"); |
6246 | 0 | else |
6247 | 0 | snprintf((char *) prefix, sizeof(prefix), "%.20s", (char *)ns->prefix); |
6248 | |
|
6249 | 0 | res = xmlSearchNsSafe(tree, prefix, &def); |
6250 | 0 | if (res < 0) |
6251 | 0 | return(NULL); |
6252 | 0 | while (def != NULL) { |
6253 | 0 | if (counter > 1000) return(NULL); |
6254 | 0 | if (ns->prefix == NULL) |
6255 | 0 | snprintf((char *) prefix, sizeof(prefix), "default%d", counter++); |
6256 | 0 | else |
6257 | 0 | snprintf((char *) prefix, sizeof(prefix), "%.20s%d", |
6258 | 0 | (char *)ns->prefix, counter++); |
6259 | 0 | res = xmlSearchNsSafe(tree, prefix, &def); |
6260 | 0 | if (res < 0) |
6261 | 0 | return(NULL); |
6262 | 0 | } |
6263 | | |
6264 | | /* |
6265 | | * OK, now we are ready to create a new one. |
6266 | | */ |
6267 | 0 | def = xmlNewNs(tree, ns->href, prefix); |
6268 | 0 | return(def); |
6269 | 0 | } |
6270 | | |
6271 | | typedef struct { |
6272 | | xmlNsPtr oldNs; |
6273 | | xmlNsPtr newNs; |
6274 | | } xmlNsCache; |
6275 | | |
6276 | | /** |
6277 | | * xmlReconciliateNs: |
6278 | | * @doc: the document |
6279 | | * @tree: a node defining the subtree to reconciliate |
6280 | | * |
6281 | | * This function checks that all the namespaces declared within the given |
6282 | | * tree are properly declared. This is needed for example after Copy or Cut |
6283 | | * and then paste operations. The subtree may still hold pointers to |
6284 | | * namespace declarations outside the subtree or invalid/masked. As much |
6285 | | * as possible the function try to reuse the existing namespaces found in |
6286 | | * the new environment. If not possible the new namespaces are redeclared |
6287 | | * on @tree at the top of the given subtree. |
6288 | | * |
6289 | | * Returns 0 on success or -1 in case of error. |
6290 | | */ |
6291 | | int |
6292 | 0 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
6293 | 0 | xmlNsCache *cache = NULL; |
6294 | 0 | int sizeCache = 0; |
6295 | 0 | int nbCache = 0; |
6296 | |
|
6297 | 0 | xmlNsPtr n; |
6298 | 0 | xmlNodePtr node = tree; |
6299 | 0 | xmlAttrPtr attr; |
6300 | 0 | int ret = 0, i; |
6301 | |
|
6302 | 0 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
6303 | 0 | if (node->doc != doc) return(-1); |
6304 | 0 | while (node != NULL) { |
6305 | | /* |
6306 | | * Reconciliate the node namespace |
6307 | | */ |
6308 | 0 | if (node->ns != NULL) { |
6309 | 0 | for (i = 0; i < nbCache; i++) { |
6310 | 0 | if (cache[i].oldNs == node->ns) { |
6311 | 0 | node->ns = cache[i].newNs; |
6312 | 0 | break; |
6313 | 0 | } |
6314 | 0 | } |
6315 | 0 | if (i == nbCache) { |
6316 | | /* |
6317 | | * OK we need to recreate a new namespace definition |
6318 | | */ |
6319 | 0 | n = xmlNewReconciledNs(tree, node->ns); |
6320 | 0 | if (n == NULL) { |
6321 | 0 | ret = -1; |
6322 | 0 | } else { |
6323 | | /* |
6324 | | * check if we need to grow the cache buffers. |
6325 | | */ |
6326 | 0 | if (sizeCache <= nbCache) { |
6327 | 0 | xmlNsCache *tmp; |
6328 | 0 | size_t newSize = sizeCache ? sizeCache * 2 : 10; |
6329 | |
|
6330 | 0 | tmp = xmlRealloc(cache, newSize * sizeof(tmp[0])); |
6331 | 0 | if (tmp == NULL) { |
6332 | 0 | ret = -1; |
6333 | 0 | } else { |
6334 | 0 | cache = tmp; |
6335 | 0 | sizeCache = newSize; |
6336 | 0 | } |
6337 | 0 | } |
6338 | 0 | if (nbCache < sizeCache) { |
6339 | 0 | cache[nbCache].newNs = n; |
6340 | 0 | cache[nbCache++].oldNs = node->ns; |
6341 | 0 | } |
6342 | 0 | } |
6343 | 0 | node->ns = n; |
6344 | 0 | } |
6345 | 0 | } |
6346 | | /* |
6347 | | * now check for namespace held by attributes on the node. |
6348 | | */ |
6349 | 0 | if (node->type == XML_ELEMENT_NODE) { |
6350 | 0 | attr = node->properties; |
6351 | 0 | while (attr != NULL) { |
6352 | 0 | if (attr->ns != NULL) { |
6353 | 0 | for (i = 0; i < nbCache; i++) { |
6354 | 0 | if (cache[i].oldNs == attr->ns) { |
6355 | 0 | attr->ns = cache[i].newNs; |
6356 | 0 | break; |
6357 | 0 | } |
6358 | 0 | } |
6359 | 0 | if (i == nbCache) { |
6360 | | /* |
6361 | | * OK we need to recreate a new namespace definition |
6362 | | */ |
6363 | 0 | n = xmlNewReconciledNs(tree, attr->ns); |
6364 | 0 | if (n == NULL) { |
6365 | 0 | ret = -1; |
6366 | 0 | } else { |
6367 | | /* |
6368 | | * check if we need to grow the cache buffers. |
6369 | | */ |
6370 | 0 | if (sizeCache <= nbCache) { |
6371 | 0 | xmlNsCache *tmp; |
6372 | 0 | size_t newSize = sizeCache ? |
6373 | 0 | sizeCache * 2 : 10; |
6374 | |
|
6375 | 0 | tmp = xmlRealloc(cache, |
6376 | 0 | newSize * sizeof(tmp[0])); |
6377 | 0 | if (tmp == NULL) { |
6378 | 0 | ret = -1; |
6379 | 0 | } else { |
6380 | 0 | cache = tmp; |
6381 | 0 | sizeCache = newSize; |
6382 | 0 | } |
6383 | 0 | } |
6384 | 0 | if (nbCache < sizeCache) { |
6385 | 0 | cache[nbCache].newNs = n; |
6386 | 0 | cache[nbCache++].oldNs = attr->ns; |
6387 | 0 | } |
6388 | 0 | } |
6389 | 0 | attr->ns = n; |
6390 | 0 | } |
6391 | 0 | } |
6392 | 0 | attr = attr->next; |
6393 | 0 | } |
6394 | 0 | } |
6395 | | |
6396 | | /* |
6397 | | * Browse the full subtree, deep first |
6398 | | */ |
6399 | 0 | if ((node->children != NULL) && (node->type != XML_ENTITY_REF_NODE)) { |
6400 | | /* deep first */ |
6401 | 0 | node = node->children; |
6402 | 0 | } else if ((node != tree) && (node->next != NULL)) { |
6403 | | /* then siblings */ |
6404 | 0 | node = node->next; |
6405 | 0 | } else if (node != tree) { |
6406 | | /* go up to parents->next if needed */ |
6407 | 0 | while (node != tree) { |
6408 | 0 | if (node->parent != NULL) |
6409 | 0 | node = node->parent; |
6410 | 0 | if ((node != tree) && (node->next != NULL)) { |
6411 | 0 | node = node->next; |
6412 | 0 | break; |
6413 | 0 | } |
6414 | 0 | if (node->parent == NULL) { |
6415 | 0 | node = NULL; |
6416 | 0 | break; |
6417 | 0 | } |
6418 | 0 | } |
6419 | | /* exit condition */ |
6420 | 0 | if (node == tree) |
6421 | 0 | node = NULL; |
6422 | 0 | } else |
6423 | 0 | break; |
6424 | 0 | } |
6425 | 0 | if (cache != NULL) |
6426 | 0 | xmlFree(cache); |
6427 | 0 | return(ret); |
6428 | 0 | } |
6429 | | |
6430 | | static xmlAttrPtr |
6431 | | xmlGetPropNodeInternal(const xmlNode *node, const xmlChar *name, |
6432 | | const xmlChar *nsName, int useDTD) |
6433 | 0 | { |
6434 | 0 | xmlAttrPtr prop; |
6435 | | |
6436 | | /* Avoid unused variable warning if features are disabled. */ |
6437 | 0 | (void) useDTD; |
6438 | |
|
6439 | 0 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
6440 | 0 | return(NULL); |
6441 | | |
6442 | 0 | if (node->properties != NULL) { |
6443 | 0 | prop = node->properties; |
6444 | 0 | if (nsName == NULL) { |
6445 | | /* |
6446 | | * We want the attr to be in no namespace. |
6447 | | */ |
6448 | 0 | do { |
6449 | 0 | if ((prop->ns == NULL) && xmlStrEqual(prop->name, name)) { |
6450 | 0 | return(prop); |
6451 | 0 | } |
6452 | 0 | prop = prop->next; |
6453 | 0 | } while (prop != NULL); |
6454 | 0 | } else { |
6455 | | /* |
6456 | | * We want the attr to be in the specified namespace. |
6457 | | */ |
6458 | 0 | do { |
6459 | 0 | if ((prop->ns != NULL) && xmlStrEqual(prop->name, name) && |
6460 | 0 | ((prop->ns->href == nsName) || |
6461 | 0 | xmlStrEqual(prop->ns->href, nsName))) |
6462 | 0 | { |
6463 | 0 | return(prop); |
6464 | 0 | } |
6465 | 0 | prop = prop->next; |
6466 | 0 | } while (prop != NULL); |
6467 | 0 | } |
6468 | 0 | } |
6469 | | |
6470 | 0 | if (! useDTD) |
6471 | 0 | return(NULL); |
6472 | | /* |
6473 | | * Check if there is a default/fixed attribute declaration in |
6474 | | * the internal or external subset. |
6475 | | */ |
6476 | 0 | if ((node->doc != NULL) && (node->doc->intSubset != NULL)) { |
6477 | 0 | xmlDocPtr doc = node->doc; |
6478 | 0 | xmlAttributePtr attrDecl = NULL; |
6479 | 0 | xmlChar *elemQName, *tmpstr = NULL; |
6480 | | |
6481 | | /* |
6482 | | * We need the QName of the element for the DTD-lookup. |
6483 | | */ |
6484 | 0 | if ((node->ns != NULL) && (node->ns->prefix != NULL)) { |
6485 | 0 | tmpstr = xmlStrdup(node->ns->prefix); |
6486 | 0 | if (tmpstr == NULL) |
6487 | 0 | return(NULL); |
6488 | 0 | tmpstr = xmlStrcat(tmpstr, BAD_CAST ":"); |
6489 | 0 | if (tmpstr == NULL) |
6490 | 0 | return(NULL); |
6491 | 0 | tmpstr = xmlStrcat(tmpstr, node->name); |
6492 | 0 | if (tmpstr == NULL) |
6493 | 0 | return(NULL); |
6494 | 0 | elemQName = tmpstr; |
6495 | 0 | } else |
6496 | 0 | elemQName = (xmlChar *) node->name; |
6497 | 0 | if (nsName == NULL) { |
6498 | | /* |
6499 | | * The common and nice case: Attr in no namespace. |
6500 | | */ |
6501 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
6502 | 0 | elemQName, name, NULL); |
6503 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
6504 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
6505 | 0 | elemQName, name, NULL); |
6506 | 0 | } |
6507 | 0 | } else if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
6508 | | /* |
6509 | | * The XML namespace must be bound to prefix 'xml'. |
6510 | | */ |
6511 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, |
6512 | 0 | elemQName, name, BAD_CAST "xml"); |
6513 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) { |
6514 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, |
6515 | 0 | elemQName, name, BAD_CAST "xml"); |
6516 | 0 | } |
6517 | 0 | } else { |
6518 | 0 | xmlNsPtr *nsList, *cur; |
6519 | | |
6520 | | /* |
6521 | | * The ugly case: Search using the prefixes of in-scope |
6522 | | * ns-decls corresponding to @nsName. |
6523 | | */ |
6524 | 0 | nsList = xmlGetNsList(node->doc, node); |
6525 | 0 | if (nsList == NULL) { |
6526 | 0 | if (tmpstr != NULL) |
6527 | 0 | xmlFree(tmpstr); |
6528 | 0 | return(NULL); |
6529 | 0 | } |
6530 | 0 | cur = nsList; |
6531 | 0 | while (*cur != NULL) { |
6532 | 0 | if (xmlStrEqual((*cur)->href, nsName)) { |
6533 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elemQName, |
6534 | 0 | name, (*cur)->prefix); |
6535 | 0 | if (attrDecl) |
6536 | 0 | break; |
6537 | 0 | if (doc->extSubset != NULL) { |
6538 | 0 | attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elemQName, |
6539 | 0 | name, (*cur)->prefix); |
6540 | 0 | if (attrDecl) |
6541 | 0 | break; |
6542 | 0 | } |
6543 | 0 | } |
6544 | 0 | cur++; |
6545 | 0 | } |
6546 | 0 | xmlFree(nsList); |
6547 | 0 | } |
6548 | 0 | if (tmpstr != NULL) |
6549 | 0 | xmlFree(tmpstr); |
6550 | | /* |
6551 | | * Only default/fixed attrs are relevant. |
6552 | | */ |
6553 | 0 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
6554 | 0 | return((xmlAttrPtr) attrDecl); |
6555 | 0 | } |
6556 | | |
6557 | 0 | return(NULL); |
6558 | 0 | } |
6559 | | |
6560 | | static xmlChar* |
6561 | | xmlGetPropNodeValueInternal(const xmlAttr *prop) |
6562 | 0 | { |
6563 | 0 | if (prop == NULL) |
6564 | 0 | return(NULL); |
6565 | 0 | if (prop->type == XML_ATTRIBUTE_NODE) { |
6566 | 0 | return(xmlNodeGetContent((xmlNodePtr) prop)); |
6567 | 0 | } else if (prop->type == XML_ATTRIBUTE_DECL) { |
6568 | 0 | return(xmlStrdup(((xmlAttributePtr)prop)->defaultValue)); |
6569 | 0 | } |
6570 | 0 | return(NULL); |
6571 | 0 | } |
6572 | | |
6573 | | /** |
6574 | | * xmlHasProp: |
6575 | | * @node: the node |
6576 | | * @name: the attribute name |
6577 | | * |
6578 | | * Search an attribute associated to a node |
6579 | | * This function also looks in DTD attribute declaration for #FIXED or |
6580 | | * default declaration values. |
6581 | | * |
6582 | | * Returns the attribute or the attribute declaration or NULL if |
6583 | | * neither was found. Also returns NULL if a memory allocation failed |
6584 | | * making this function unreliable. |
6585 | | */ |
6586 | | xmlAttrPtr |
6587 | 0 | xmlHasProp(const xmlNode *node, const xmlChar *name) { |
6588 | 0 | xmlAttrPtr prop; |
6589 | 0 | xmlDocPtr doc; |
6590 | |
|
6591 | 0 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE) || (name == NULL)) |
6592 | 0 | return(NULL); |
6593 | | /* |
6594 | | * Check on the properties attached to the node |
6595 | | */ |
6596 | 0 | prop = node->properties; |
6597 | 0 | while (prop != NULL) { |
6598 | 0 | if (xmlStrEqual(prop->name, name)) { |
6599 | 0 | return(prop); |
6600 | 0 | } |
6601 | 0 | prop = prop->next; |
6602 | 0 | } |
6603 | | |
6604 | | /* |
6605 | | * Check if there is a default declaration in the internal |
6606 | | * or external subsets |
6607 | | */ |
6608 | 0 | doc = node->doc; |
6609 | 0 | if (doc != NULL) { |
6610 | 0 | xmlAttributePtr attrDecl; |
6611 | 0 | if (doc->intSubset != NULL) { |
6612 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->intSubset, node->name, name); |
6613 | 0 | if ((attrDecl == NULL) && (doc->extSubset != NULL)) |
6614 | 0 | attrDecl = xmlGetDtdAttrDesc(doc->extSubset, node->name, name); |
6615 | 0 | if ((attrDecl != NULL) && (attrDecl->defaultValue != NULL)) |
6616 | | /* return attribute declaration only if a default value is given |
6617 | | (that includes #FIXED declarations) */ |
6618 | 0 | return((xmlAttrPtr) attrDecl); |
6619 | 0 | } |
6620 | 0 | } |
6621 | 0 | return(NULL); |
6622 | 0 | } |
6623 | | |
6624 | | /** |
6625 | | * xmlHasNsProp: |
6626 | | * @node: the node |
6627 | | * @name: the attribute name |
6628 | | * @nameSpace: the URI of the namespace |
6629 | | * |
6630 | | * Search for an attribute associated to a node |
6631 | | * This attribute has to be anchored in the namespace specified. |
6632 | | * This does the entity substitution. |
6633 | | * This function looks in DTD attribute declaration for #FIXED or |
6634 | | * default declaration values. |
6635 | | * Note that a namespace of NULL indicates to use the default namespace. |
6636 | | * |
6637 | | * Returns the attribute or the attribute declaration or NULL if |
6638 | | * neither was found. Also returns NULL if a memory allocation failed |
6639 | | * making this function unreliable. |
6640 | | */ |
6641 | | xmlAttrPtr |
6642 | 0 | xmlHasNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
6643 | |
|
6644 | 0 | return(xmlGetPropNodeInternal(node, name, nameSpace, 1)); |
6645 | 0 | } |
6646 | | |
6647 | | /** |
6648 | | * xmlNodeGetAttrValue: |
6649 | | * @node: the node |
6650 | | * @name: the attribute name |
6651 | | * @nsUri: the URI of the namespace |
6652 | | * @out: the returned string |
6653 | | * |
6654 | | * Search and get the value of an attribute associated to a node |
6655 | | * This attribute has to be anchored in the namespace specified. |
6656 | | * This does the entity substitution. The returned value must be |
6657 | | * freed by the caller. |
6658 | | * |
6659 | | * Available since 2.13.0. |
6660 | | * |
6661 | | * Returns 0 on success, 1 if no attribute was found, -1 if a |
6662 | | * memory allocation failed. |
6663 | | */ |
6664 | | int |
6665 | | xmlNodeGetAttrValue(const xmlNode *node, const xmlChar *name, |
6666 | 0 | const xmlChar *nsUri, xmlChar **out) { |
6667 | 0 | xmlAttrPtr prop; |
6668 | |
|
6669 | 0 | if (out == NULL) |
6670 | 0 | return(1); |
6671 | 0 | *out = NULL; |
6672 | |
|
6673 | 0 | prop = xmlGetPropNodeInternal(node, name, nsUri, 0); |
6674 | 0 | if (prop == NULL) |
6675 | 0 | return(1); |
6676 | | |
6677 | 0 | *out = xmlGetPropNodeValueInternal(prop); |
6678 | 0 | if (*out == NULL) |
6679 | 0 | return(-1); |
6680 | 0 | return(0); |
6681 | 0 | } |
6682 | | |
6683 | | /** |
6684 | | * xmlGetProp: |
6685 | | * @node: the node |
6686 | | * @name: the attribute name |
6687 | | * |
6688 | | * Search and get the value of an attribute associated to a node |
6689 | | * This does the entity substitution. |
6690 | | * This function looks in DTD attribute declaration for #FIXED or |
6691 | | * default declaration values. |
6692 | | * |
6693 | | * NOTE: This function acts independently of namespaces associated |
6694 | | * to the attribute. Use xmlGetNsProp() or xmlGetNoNsProp() |
6695 | | * for namespace aware processing. |
6696 | | * |
6697 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6698 | | * missing attributes. It's more robust to use xmlNodeGetAttrValue. |
6699 | | * |
6700 | | * Returns the attribute value or NULL if not found or a memory allocation |
6701 | | * failed. It's up to the caller to free the memory with xmlFree(). |
6702 | | */ |
6703 | | xmlChar * |
6704 | 0 | xmlGetProp(const xmlNode *node, const xmlChar *name) { |
6705 | 0 | xmlAttrPtr prop; |
6706 | |
|
6707 | 0 | prop = xmlHasProp(node, name); |
6708 | 0 | if (prop == NULL) |
6709 | 0 | return(NULL); |
6710 | 0 | return(xmlGetPropNodeValueInternal(prop)); |
6711 | 0 | } |
6712 | | |
6713 | | /** |
6714 | | * xmlGetNoNsProp: |
6715 | | * @node: the node |
6716 | | * @name: the attribute name |
6717 | | * |
6718 | | * Search and get the value of an attribute associated to a node |
6719 | | * This does the entity substitution. |
6720 | | * This function looks in DTD attribute declaration for #FIXED or |
6721 | | * default declaration values. |
6722 | | * This function is similar to xmlGetProp except it will accept only |
6723 | | * an attribute in no namespace. |
6724 | | * |
6725 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6726 | | * missing attributes. It's more robust to use xmlNodeGetAttrValue. |
6727 | | * |
6728 | | * Returns the attribute value or NULL if not found or a memory allocation |
6729 | | * failed. It's up to the caller to free the memory with xmlFree(). |
6730 | | */ |
6731 | | xmlChar * |
6732 | 0 | xmlGetNoNsProp(const xmlNode *node, const xmlChar *name) { |
6733 | 0 | xmlAttrPtr prop; |
6734 | |
|
6735 | 0 | prop = xmlGetPropNodeInternal(node, name, NULL, 1); |
6736 | 0 | if (prop == NULL) |
6737 | 0 | return(NULL); |
6738 | 0 | return(xmlGetPropNodeValueInternal(prop)); |
6739 | 0 | } |
6740 | | |
6741 | | /** |
6742 | | * xmlGetNsProp: |
6743 | | * @node: the node |
6744 | | * @name: the attribute name |
6745 | | * @nameSpace: the URI of the namespace |
6746 | | * |
6747 | | * Search and get the value of an attribute associated to a node |
6748 | | * This attribute has to be anchored in the namespace specified. |
6749 | | * This does the entity substitution. |
6750 | | * This function looks in DTD attribute declaration for #FIXED or |
6751 | | * default declaration values. |
6752 | | * |
6753 | | * NOTE: This function doesn't allow to distinguish malloc failures from |
6754 | | * missing attributes. It's more robust to use xmlNodeGetAttrValue. |
6755 | | * |
6756 | | * Returns the attribute value or NULL if not found or a memory allocation |
6757 | | * failed. It's up to the caller to free the memory with xmlFree(). |
6758 | | */ |
6759 | | xmlChar * |
6760 | 0 | xmlGetNsProp(const xmlNode *node, const xmlChar *name, const xmlChar *nameSpace) { |
6761 | 0 | xmlAttrPtr prop; |
6762 | |
|
6763 | 0 | prop = xmlGetPropNodeInternal(node, name, nameSpace, 1); |
6764 | 0 | if (prop == NULL) |
6765 | 0 | return(NULL); |
6766 | 0 | return(xmlGetPropNodeValueInternal(prop)); |
6767 | 0 | } |
6768 | | |
6769 | | /** |
6770 | | * xmlUnsetProp: |
6771 | | * @node: the node |
6772 | | * @name: the attribute name |
6773 | | * |
6774 | | * Remove an attribute carried by a node. |
6775 | | * This handles only attributes in no namespace. |
6776 | | * Returns 0 if successful, -1 if not found |
6777 | | */ |
6778 | | int |
6779 | 0 | xmlUnsetProp(xmlNodePtr node, const xmlChar *name) { |
6780 | 0 | xmlAttrPtr prop; |
6781 | |
|
6782 | 0 | prop = xmlGetPropNodeInternal(node, name, NULL, 0); |
6783 | 0 | if (prop == NULL) |
6784 | 0 | return(-1); |
6785 | 0 | xmlUnlinkNodeInternal((xmlNodePtr) prop); |
6786 | 0 | xmlFreeProp(prop); |
6787 | 0 | return(0); |
6788 | 0 | } |
6789 | | |
6790 | | /** |
6791 | | * xmlUnsetNsProp: |
6792 | | * @node: the node |
6793 | | * @ns: the namespace definition |
6794 | | * @name: the attribute name |
6795 | | * |
6796 | | * Remove an attribute carried by a node. |
6797 | | * Returns 0 if successful, -1 if not found |
6798 | | */ |
6799 | | int |
6800 | 0 | xmlUnsetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name) { |
6801 | 0 | xmlAttrPtr prop; |
6802 | |
|
6803 | 0 | prop = xmlGetPropNodeInternal(node, name, |
6804 | 0 | (ns != NULL) ? ns->href : NULL, 0); |
6805 | 0 | if (prop == NULL) |
6806 | 0 | return(-1); |
6807 | 0 | xmlUnlinkNodeInternal((xmlNodePtr) prop); |
6808 | 0 | xmlFreeProp(prop); |
6809 | 0 | return(0); |
6810 | 0 | } |
6811 | | |
6812 | | /** |
6813 | | * xmlSetProp: |
6814 | | * @node: the node |
6815 | | * @name: the attribute name (a QName) |
6816 | | * @value: the attribute value |
6817 | | * |
6818 | | * Set (or reset) an attribute carried by a node. |
6819 | | * If @name has a prefix, then the corresponding |
6820 | | * namespace-binding will be used, if in scope; it is an |
6821 | | * error it there's no such ns-binding for the prefix in |
6822 | | * scope. |
6823 | | * Returns the attribute pointer. |
6824 | | * |
6825 | | */ |
6826 | | xmlAttrPtr |
6827 | 0 | xmlSetProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) { |
6828 | 0 | xmlNsPtr ns = NULL; |
6829 | 0 | const xmlChar *localname; |
6830 | 0 | xmlChar *prefix; |
6831 | 0 | int res; |
6832 | |
|
6833 | 0 | if ((node == NULL) || (name == NULL) || (node->type != XML_ELEMENT_NODE)) |
6834 | 0 | return(NULL); |
6835 | | |
6836 | | /* |
6837 | | * handle QNames |
6838 | | */ |
6839 | 0 | localname = xmlSplitQName4(name, &prefix); |
6840 | 0 | if (localname == NULL) |
6841 | 0 | return(NULL); |
6842 | | |
6843 | 0 | if (prefix != NULL) { |
6844 | 0 | res = xmlSearchNsSafe(node, prefix, &ns); |
6845 | 0 | xmlFree(prefix); |
6846 | 0 | if (res < 0) |
6847 | 0 | return(NULL); |
6848 | 0 | if (ns != NULL) |
6849 | 0 | return(xmlSetNsProp(node, ns, localname, value)); |
6850 | 0 | } |
6851 | | |
6852 | 0 | return(xmlSetNsProp(node, NULL, name, value)); |
6853 | 0 | } |
6854 | | |
6855 | | /** |
6856 | | * xmlSetNsProp: |
6857 | | * @node: the node |
6858 | | * @ns: the namespace definition |
6859 | | * @name: the attribute name |
6860 | | * @value: the attribute value |
6861 | | * |
6862 | | * Set (or reset) an attribute carried by a node. |
6863 | | * The ns structure must be in scope, this is not checked |
6864 | | * |
6865 | | * Returns the attribute pointer. |
6866 | | */ |
6867 | | xmlAttrPtr |
6868 | | xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, |
6869 | | const xmlChar *value) |
6870 | 0 | { |
6871 | 0 | xmlAttrPtr prop; |
6872 | |
|
6873 | 0 | if (ns && (ns->href == NULL)) |
6874 | 0 | return(NULL); |
6875 | 0 | if (name == NULL) |
6876 | 0 | return(NULL); |
6877 | 0 | prop = xmlGetPropNodeInternal(node, name, |
6878 | 0 | (ns != NULL) ? ns->href : NULL, 0); |
6879 | 0 | if (prop != NULL) { |
6880 | 0 | xmlNodePtr children = NULL; |
6881 | | |
6882 | | /* |
6883 | | * Modify the attribute's value. |
6884 | | */ |
6885 | 0 | if (value != NULL) { |
6886 | 0 | children = xmlNewDocText(node->doc, value); |
6887 | 0 | if (children == NULL) |
6888 | 0 | return(NULL); |
6889 | 0 | } |
6890 | | |
6891 | 0 | if (prop->atype == XML_ATTRIBUTE_ID) { |
6892 | 0 | xmlRemoveID(node->doc, prop); |
6893 | 0 | prop->atype = XML_ATTRIBUTE_ID; |
6894 | 0 | } |
6895 | 0 | if (prop->children != NULL) |
6896 | 0 | xmlFreeNodeList(prop->children); |
6897 | 0 | prop->children = NULL; |
6898 | 0 | prop->last = NULL; |
6899 | 0 | prop->ns = ns; |
6900 | 0 | if (value != NULL) { |
6901 | 0 | xmlNodePtr tmp; |
6902 | |
|
6903 | 0 | prop->children = children; |
6904 | 0 | prop->last = NULL; |
6905 | 0 | tmp = prop->children; |
6906 | 0 | while (tmp != NULL) { |
6907 | 0 | tmp->parent = (xmlNodePtr) prop; |
6908 | 0 | if (tmp->next == NULL) |
6909 | 0 | prop->last = tmp; |
6910 | 0 | tmp = tmp->next; |
6911 | 0 | } |
6912 | 0 | } |
6913 | 0 | if ((prop->atype == XML_ATTRIBUTE_ID) && |
6914 | 0 | (xmlAddIDSafe(prop, value) < 0)) { |
6915 | 0 | return(NULL); |
6916 | 0 | } |
6917 | 0 | return(prop); |
6918 | 0 | } |
6919 | | /* |
6920 | | * No equal attr found; create a new one. |
6921 | | */ |
6922 | 0 | return(xmlNewPropInternal(node, ns, name, value, 0)); |
6923 | 0 | } |
6924 | | |
6925 | | /** |
6926 | | * xmlNodeIsText: |
6927 | | * @node: the node |
6928 | | * |
6929 | | * Is this node a Text node ? |
6930 | | * Returns 1 yes, 0 no |
6931 | | */ |
6932 | | int |
6933 | 0 | xmlNodeIsText(const xmlNode *node) { |
6934 | 0 | if (node == NULL) return(0); |
6935 | | |
6936 | 0 | if (node->type == XML_TEXT_NODE) return(1); |
6937 | 0 | return(0); |
6938 | 0 | } |
6939 | | |
6940 | | /** |
6941 | | * xmlIsBlankNode: |
6942 | | * @node: the node |
6943 | | * |
6944 | | * Checks whether this node is an empty or whitespace only |
6945 | | * (and possibly ignorable) text-node. |
6946 | | * |
6947 | | * Returns 1 yes, 0 no |
6948 | | */ |
6949 | | int |
6950 | 0 | xmlIsBlankNode(const xmlNode *node) { |
6951 | 0 | const xmlChar *cur; |
6952 | 0 | if (node == NULL) return(0); |
6953 | | |
6954 | 0 | if ((node->type != XML_TEXT_NODE) && |
6955 | 0 | (node->type != XML_CDATA_SECTION_NODE)) |
6956 | 0 | return(0); |
6957 | 0 | if (node->content == NULL) return(1); |
6958 | 0 | cur = node->content; |
6959 | 0 | while (*cur != 0) { |
6960 | 0 | if (!IS_BLANK_CH(*cur)) return(0); |
6961 | 0 | cur++; |
6962 | 0 | } |
6963 | | |
6964 | 0 | return(1); |
6965 | 0 | } |
6966 | | |
6967 | | /** |
6968 | | * xmlTextConcat: |
6969 | | * @node: the node |
6970 | | * @content: the content |
6971 | | * @len: @content length |
6972 | | * |
6973 | | * Concat the given string at the end of the existing node content. |
6974 | | * |
6975 | | * If @len is -1, the string length will be calculated. |
6976 | | * |
6977 | | * Returns -1 in case of error, 0 otherwise |
6978 | | */ |
6979 | | |
6980 | | int |
6981 | 0 | xmlTextConcat(xmlNodePtr node, const xmlChar *content, int len) { |
6982 | 0 | if (node == NULL) |
6983 | 0 | return(-1); |
6984 | | |
6985 | 0 | if ((node->type != XML_TEXT_NODE) && |
6986 | 0 | (node->type != XML_CDATA_SECTION_NODE) && |
6987 | 0 | (node->type != XML_COMMENT_NODE) && |
6988 | 0 | (node->type != XML_PI_NODE)) |
6989 | 0 | return(-1); |
6990 | | |
6991 | 0 | return(xmlTextAddContent(node, content, len)); |
6992 | 0 | } |
6993 | | |
6994 | | /** |
6995 | | * xmlGetDocCompressMode: |
6996 | | * @doc: the document |
6997 | | * |
6998 | | * get the compression ratio for a document, ZLIB based |
6999 | | * Returns 0 (uncompressed) to 9 (max compression) |
7000 | | */ |
7001 | | int |
7002 | 0 | xmlGetDocCompressMode (const xmlDoc *doc) { |
7003 | 0 | if (doc == NULL) return(-1); |
7004 | 0 | return(doc->compression); |
7005 | 0 | } |
7006 | | |
7007 | | /** |
7008 | | * xmlSetDocCompressMode: |
7009 | | * @doc: the document |
7010 | | * @mode: the compression ratio |
7011 | | * |
7012 | | * set the compression ratio for a document, ZLIB based |
7013 | | * Correct values: 0 (uncompressed) to 9 (max compression) |
7014 | | */ |
7015 | | void |
7016 | 0 | xmlSetDocCompressMode (xmlDocPtr doc, int mode) { |
7017 | 0 | if (doc == NULL) return; |
7018 | 0 | if (mode < 0) doc->compression = 0; |
7019 | 0 | else if (mode > 9) doc->compression = 9; |
7020 | 0 | else doc->compression = mode; |
7021 | 0 | } |
7022 | | |
7023 | | /** |
7024 | | * xmlGetCompressMode: |
7025 | | * |
7026 | | * DEPRECATED: Use xmlGetDocCompressMode |
7027 | | * |
7028 | | * get the default compression mode used, ZLIB based. |
7029 | | * Returns 0 (uncompressed) to 9 (max compression) |
7030 | | */ |
7031 | | int |
7032 | | xmlGetCompressMode(void) |
7033 | 0 | { |
7034 | 0 | return (xmlCompressMode); |
7035 | 0 | } |
7036 | | |
7037 | | /** |
7038 | | * xmlSetCompressMode: |
7039 | | * @mode: the compression ratio |
7040 | | * |
7041 | | * DEPRECATED: Use xmlSetDocCompressMode |
7042 | | * |
7043 | | * set the default compression mode used, ZLIB based |
7044 | | * Correct values: 0 (uncompressed) to 9 (max compression) |
7045 | | */ |
7046 | | void |
7047 | 0 | xmlSetCompressMode(int mode) { |
7048 | 0 | if (mode < 0) xmlCompressMode = 0; |
7049 | 0 | else if (mode > 9) xmlCompressMode = 9; |
7050 | 0 | else xmlCompressMode = mode; |
7051 | 0 | } |
7052 | | |
7053 | 0 | #define XML_TREE_NSMAP_PARENT -1 |
7054 | | #define XML_TREE_NSMAP_XML -2 |
7055 | 0 | #define XML_TREE_NSMAP_DOC -3 |
7056 | 0 | #define XML_TREE_NSMAP_CUSTOM -4 |
7057 | | |
7058 | | typedef struct xmlNsMapItem *xmlNsMapItemPtr; |
7059 | | struct xmlNsMapItem { |
7060 | | xmlNsMapItemPtr next; |
7061 | | xmlNsMapItemPtr prev; |
7062 | | xmlNsPtr oldNs; /* old ns decl reference */ |
7063 | | xmlNsPtr newNs; /* new ns decl reference */ |
7064 | | int shadowDepth; /* Shadowed at this depth */ |
7065 | | /* |
7066 | | * depth: |
7067 | | * >= 0 == @node's ns-decls |
7068 | | * -1 == @parent's ns-decls |
7069 | | * -2 == the doc->oldNs XML ns-decl |
7070 | | * -3 == the doc->oldNs storage ns-decls |
7071 | | * -4 == ns-decls provided via custom ns-handling |
7072 | | */ |
7073 | | int depth; |
7074 | | }; |
7075 | | |
7076 | | typedef struct xmlNsMap *xmlNsMapPtr; |
7077 | | struct xmlNsMap { |
7078 | | xmlNsMapItemPtr first; |
7079 | | xmlNsMapItemPtr last; |
7080 | | xmlNsMapItemPtr pool; |
7081 | | }; |
7082 | | |
7083 | 0 | #define XML_NSMAP_NOTEMPTY(m) (((m) != NULL) && ((m)->first != NULL)) |
7084 | 0 | #define XML_NSMAP_FOREACH(m, i) for (i = (m)->first; i != NULL; i = (i)->next) |
7085 | | #define XML_NSMAP_POP(m, i) \ |
7086 | 0 | i = (m)->last; \ |
7087 | 0 | (m)->last = (i)->prev; \ |
7088 | 0 | if ((m)->last == NULL) \ |
7089 | 0 | (m)->first = NULL; \ |
7090 | 0 | else \ |
7091 | 0 | (m)->last->next = NULL; \ |
7092 | 0 | (i)->next = (m)->pool; \ |
7093 | 0 | (m)->pool = i; |
7094 | | |
7095 | | /* |
7096 | | * xmlDOMWrapNsMapFree: |
7097 | | * @map: the ns-map |
7098 | | * |
7099 | | * Frees the ns-map |
7100 | | */ |
7101 | | static void |
7102 | | xmlDOMWrapNsMapFree(xmlNsMapPtr nsmap) |
7103 | 0 | { |
7104 | 0 | xmlNsMapItemPtr cur, tmp; |
7105 | |
|
7106 | 0 | if (nsmap == NULL) |
7107 | 0 | return; |
7108 | 0 | cur = nsmap->pool; |
7109 | 0 | while (cur != NULL) { |
7110 | 0 | tmp = cur; |
7111 | 0 | cur = cur->next; |
7112 | 0 | xmlFree(tmp); |
7113 | 0 | } |
7114 | 0 | cur = nsmap->first; |
7115 | 0 | while (cur != NULL) { |
7116 | 0 | tmp = cur; |
7117 | 0 | cur = cur->next; |
7118 | 0 | xmlFree(tmp); |
7119 | 0 | } |
7120 | 0 | xmlFree(nsmap); |
7121 | 0 | } |
7122 | | |
7123 | | /* |
7124 | | * xmlDOMWrapNsMapAddItem: |
7125 | | * @map: the ns-map |
7126 | | * @oldNs: the old ns-struct |
7127 | | * @newNs: the new ns-struct |
7128 | | * @depth: depth and ns-kind information |
7129 | | * |
7130 | | * Adds an ns-mapping item. |
7131 | | */ |
7132 | | static xmlNsMapItemPtr |
7133 | | xmlDOMWrapNsMapAddItem(xmlNsMapPtr *nsmap, int position, |
7134 | | xmlNsPtr oldNs, xmlNsPtr newNs, int depth) |
7135 | 0 | { |
7136 | 0 | xmlNsMapItemPtr ret; |
7137 | 0 | xmlNsMapPtr map; |
7138 | |
|
7139 | 0 | if (nsmap == NULL) |
7140 | 0 | return(NULL); |
7141 | 0 | if ((position != -1) && (position != 0)) |
7142 | 0 | return(NULL); |
7143 | 0 | map = *nsmap; |
7144 | |
|
7145 | 0 | if (map == NULL) { |
7146 | | /* |
7147 | | * Create the ns-map. |
7148 | | */ |
7149 | 0 | map = (xmlNsMapPtr) xmlMalloc(sizeof(struct xmlNsMap)); |
7150 | 0 | if (map == NULL) |
7151 | 0 | return(NULL); |
7152 | 0 | memset(map, 0, sizeof(struct xmlNsMap)); |
7153 | 0 | *nsmap = map; |
7154 | 0 | } |
7155 | | |
7156 | 0 | if (map->pool != NULL) { |
7157 | | /* |
7158 | | * Reuse an item from the pool. |
7159 | | */ |
7160 | 0 | ret = map->pool; |
7161 | 0 | map->pool = ret->next; |
7162 | 0 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
7163 | 0 | } else { |
7164 | | /* |
7165 | | * Create a new item. |
7166 | | */ |
7167 | 0 | ret = (xmlNsMapItemPtr) xmlMalloc(sizeof(struct xmlNsMapItem)); |
7168 | 0 | if (ret == NULL) |
7169 | 0 | return(NULL); |
7170 | 0 | memset(ret, 0, sizeof(struct xmlNsMapItem)); |
7171 | 0 | } |
7172 | | |
7173 | 0 | if (map->first == NULL) { |
7174 | | /* |
7175 | | * First ever. |
7176 | | */ |
7177 | 0 | map->first = ret; |
7178 | 0 | map->last = ret; |
7179 | 0 | } else if (position == -1) { |
7180 | | /* |
7181 | | * Append. |
7182 | | */ |
7183 | 0 | ret->prev = map->last; |
7184 | 0 | map->last->next = ret; |
7185 | 0 | map->last = ret; |
7186 | 0 | } else if (position == 0) { |
7187 | | /* |
7188 | | * Set on first position. |
7189 | | */ |
7190 | 0 | map->first->prev = ret; |
7191 | 0 | ret->next = map->first; |
7192 | 0 | map->first = ret; |
7193 | 0 | } |
7194 | |
|
7195 | 0 | ret->oldNs = oldNs; |
7196 | 0 | ret->newNs = newNs; |
7197 | 0 | ret->shadowDepth = -1; |
7198 | 0 | ret->depth = depth; |
7199 | 0 | return (ret); |
7200 | 0 | } |
7201 | | |
7202 | | /* |
7203 | | * xmlDOMWrapStoreNs: |
7204 | | * @doc: the doc |
7205 | | * @nsName: the namespace name |
7206 | | * @prefix: the prefix |
7207 | | * |
7208 | | * Creates or reuses an xmlNs struct on doc->oldNs with |
7209 | | * the given prefix and namespace name. |
7210 | | * |
7211 | | * Returns the acquired ns struct or NULL in case of an API |
7212 | | * or internal error. |
7213 | | */ |
7214 | | static xmlNsPtr |
7215 | | xmlDOMWrapStoreNs(xmlDocPtr doc, |
7216 | | const xmlChar *nsName, |
7217 | | const xmlChar *prefix) |
7218 | 0 | { |
7219 | 0 | xmlNsPtr ns; |
7220 | |
|
7221 | 0 | if (doc == NULL) |
7222 | 0 | return (NULL); |
7223 | 0 | ns = xmlTreeEnsureXMLDecl(doc); |
7224 | 0 | if (ns == NULL) |
7225 | 0 | return (NULL); |
7226 | 0 | if (ns->next != NULL) { |
7227 | | /* Reuse. */ |
7228 | 0 | ns = ns->next; |
7229 | 0 | while (ns != NULL) { |
7230 | 0 | if (((ns->prefix == prefix) || |
7231 | 0 | xmlStrEqual(ns->prefix, prefix)) && |
7232 | 0 | xmlStrEqual(ns->href, nsName)) { |
7233 | 0 | return (ns); |
7234 | 0 | } |
7235 | 0 | if (ns->next == NULL) |
7236 | 0 | break; |
7237 | 0 | ns = ns->next; |
7238 | 0 | } |
7239 | 0 | } |
7240 | | /* Create. */ |
7241 | 0 | if (ns != NULL) { |
7242 | 0 | ns->next = xmlNewNs(NULL, nsName, prefix); |
7243 | 0 | return (ns->next); |
7244 | 0 | } |
7245 | 0 | return(NULL); |
7246 | 0 | } |
7247 | | |
7248 | | /* |
7249 | | * xmlDOMWrapNewCtxt: |
7250 | | * |
7251 | | * Allocates and initializes a new DOM-wrapper context. |
7252 | | * |
7253 | | * Returns the xmlDOMWrapCtxtPtr or NULL in case of an internal error. |
7254 | | */ |
7255 | | xmlDOMWrapCtxtPtr |
7256 | | xmlDOMWrapNewCtxt(void) |
7257 | 0 | { |
7258 | 0 | xmlDOMWrapCtxtPtr ret; |
7259 | |
|
7260 | 0 | ret = xmlMalloc(sizeof(xmlDOMWrapCtxt)); |
7261 | 0 | if (ret == NULL) |
7262 | 0 | return (NULL); |
7263 | 0 | memset(ret, 0, sizeof(xmlDOMWrapCtxt)); |
7264 | 0 | return (ret); |
7265 | 0 | } |
7266 | | |
7267 | | /* |
7268 | | * xmlDOMWrapFreeCtxt: |
7269 | | * @ctxt: the DOM-wrapper context |
7270 | | * |
7271 | | * Frees the DOM-wrapper context. |
7272 | | */ |
7273 | | void |
7274 | | xmlDOMWrapFreeCtxt(xmlDOMWrapCtxtPtr ctxt) |
7275 | 0 | { |
7276 | 0 | if (ctxt == NULL) |
7277 | 0 | return; |
7278 | 0 | if (ctxt->namespaceMap != NULL) |
7279 | 0 | xmlDOMWrapNsMapFree((xmlNsMapPtr) ctxt->namespaceMap); |
7280 | | /* |
7281 | | * TODO: Store the namespace map in the context. |
7282 | | */ |
7283 | 0 | xmlFree(ctxt); |
7284 | 0 | } |
7285 | | |
7286 | | /* |
7287 | | * xmlTreeLookupNsListByPrefix: |
7288 | | * @nsList: a list of ns-structs |
7289 | | * @prefix: the searched prefix |
7290 | | * |
7291 | | * Searches for a ns-decl with the given prefix in @nsList. |
7292 | | * |
7293 | | * Returns the ns-decl if found, NULL if not found and on |
7294 | | * API errors. |
7295 | | */ |
7296 | | static xmlNsPtr |
7297 | | xmlTreeNSListLookupByPrefix(xmlNsPtr nsList, const xmlChar *prefix) |
7298 | 0 | { |
7299 | 0 | if (nsList == NULL) |
7300 | 0 | return (NULL); |
7301 | 0 | { |
7302 | 0 | xmlNsPtr ns; |
7303 | 0 | ns = nsList; |
7304 | 0 | do { |
7305 | 0 | if ((prefix == ns->prefix) || |
7306 | 0 | xmlStrEqual(prefix, ns->prefix)) { |
7307 | 0 | return (ns); |
7308 | 0 | } |
7309 | 0 | ns = ns->next; |
7310 | 0 | } while (ns != NULL); |
7311 | 0 | } |
7312 | 0 | return (NULL); |
7313 | 0 | } |
7314 | | |
7315 | | /* |
7316 | | * |
7317 | | * xmlDOMWrapNSNormGatherInScopeNs: |
7318 | | * @map: the namespace map |
7319 | | * @node: the node to start with |
7320 | | * |
7321 | | * Puts in-scope namespaces into the ns-map. |
7322 | | * |
7323 | | * Returns 0 on success, -1 on API or internal errors. |
7324 | | */ |
7325 | | static int |
7326 | | xmlDOMWrapNSNormGatherInScopeNs(xmlNsMapPtr *map, |
7327 | | xmlNodePtr node) |
7328 | 0 | { |
7329 | 0 | xmlNodePtr cur; |
7330 | 0 | xmlNsPtr ns; |
7331 | 0 | xmlNsMapItemPtr mi; |
7332 | 0 | int shadowed; |
7333 | |
|
7334 | 0 | if ((map == NULL) || (*map != NULL)) |
7335 | 0 | return (-1); |
7336 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
7337 | 0 | return (-1); |
7338 | | /* |
7339 | | * Get in-scope ns-decls of @parent. |
7340 | | */ |
7341 | 0 | cur = node; |
7342 | 0 | while ((cur != NULL) && (cur != (xmlNodePtr) cur->doc)) { |
7343 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7344 | 0 | if (cur->nsDef != NULL) { |
7345 | 0 | ns = cur->nsDef; |
7346 | 0 | do { |
7347 | 0 | shadowed = 0; |
7348 | 0 | if (XML_NSMAP_NOTEMPTY(*map)) { |
7349 | | /* |
7350 | | * Skip shadowed prefixes. |
7351 | | */ |
7352 | 0 | XML_NSMAP_FOREACH(*map, mi) { |
7353 | 0 | if ((ns->prefix == mi->newNs->prefix) || |
7354 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) { |
7355 | 0 | shadowed = 1; |
7356 | 0 | break; |
7357 | 0 | } |
7358 | 0 | } |
7359 | 0 | } |
7360 | | /* |
7361 | | * Insert mapping. |
7362 | | */ |
7363 | 0 | mi = xmlDOMWrapNsMapAddItem(map, 0, NULL, |
7364 | 0 | ns, XML_TREE_NSMAP_PARENT); |
7365 | 0 | if (mi == NULL) |
7366 | 0 | return (-1); |
7367 | 0 | if (shadowed) |
7368 | 0 | mi->shadowDepth = 0; |
7369 | 0 | ns = ns->next; |
7370 | 0 | } while (ns != NULL); |
7371 | 0 | } |
7372 | 0 | } |
7373 | 0 | cur = cur->parent; |
7374 | 0 | } |
7375 | 0 | return (0); |
7376 | 0 | } |
7377 | | |
7378 | | /* |
7379 | | * xmlDOMWrapNSNormAddNsMapItem2: |
7380 | | * |
7381 | | * For internal use. Adds a ns-decl mapping. |
7382 | | * |
7383 | | * Returns 0 on success, -1 on internal errors. |
7384 | | */ |
7385 | | static int |
7386 | | xmlDOMWrapNSNormAddNsMapItem2(xmlNsPtr **list, int *size, int *number, |
7387 | | xmlNsPtr oldNs, xmlNsPtr newNs) |
7388 | 0 | { |
7389 | 0 | if (*number >= *size) { |
7390 | 0 | xmlNsPtr *tmp; |
7391 | 0 | size_t newSize; |
7392 | |
|
7393 | 0 | newSize = *size ? *size * 2 : 3; |
7394 | 0 | tmp = xmlRealloc(*list, newSize * 2 * sizeof(tmp[0])); |
7395 | 0 | if (tmp == NULL) |
7396 | 0 | return(-1); |
7397 | 0 | *list = tmp; |
7398 | 0 | *size = newSize; |
7399 | 0 | } |
7400 | | |
7401 | 0 | (*list)[2 * (*number)] = oldNs; |
7402 | 0 | (*list)[2 * (*number) +1] = newNs; |
7403 | 0 | (*number)++; |
7404 | 0 | return (0); |
7405 | 0 | } |
7406 | | |
7407 | | /* |
7408 | | * xmlDOMWrapRemoveNode: |
7409 | | * @ctxt: a DOM wrapper context |
7410 | | * @doc: the doc |
7411 | | * @node: the node to be removed. |
7412 | | * @options: set of options, unused at the moment |
7413 | | * |
7414 | | * Unlinks the given node from its owner. |
7415 | | * This will substitute ns-references to node->nsDef for |
7416 | | * ns-references to doc->oldNs, thus ensuring the removed |
7417 | | * branch to be autark wrt ns-references. |
7418 | | * |
7419 | | * NOTE: This function was not intensively tested. |
7420 | | * |
7421 | | * Returns 0 on success, 1 if the node is not supported, |
7422 | | * -1 on API and internal errors. |
7423 | | */ |
7424 | | int |
7425 | | xmlDOMWrapRemoveNode(xmlDOMWrapCtxtPtr ctxt, xmlDocPtr doc, |
7426 | | xmlNodePtr node, int options ATTRIBUTE_UNUSED) |
7427 | 0 | { |
7428 | 0 | xmlNsPtr *list = NULL; |
7429 | 0 | int sizeList = 0, nbList = 0, ret = 0, i, j; |
7430 | 0 | xmlNsPtr ns; |
7431 | |
|
7432 | 0 | if ((node == NULL) || (doc == NULL) || (node->doc != doc)) |
7433 | 0 | return (-1); |
7434 | | |
7435 | | /* TODO: 0 or -1 ? */ |
7436 | 0 | if (node->parent == NULL) |
7437 | 0 | return (0); |
7438 | | |
7439 | 0 | switch (node->type) { |
7440 | 0 | case XML_TEXT_NODE: |
7441 | 0 | case XML_CDATA_SECTION_NODE: |
7442 | 0 | case XML_ENTITY_REF_NODE: |
7443 | 0 | case XML_PI_NODE: |
7444 | 0 | case XML_COMMENT_NODE: |
7445 | 0 | xmlUnlinkNodeInternal(node); |
7446 | 0 | return (0); |
7447 | 0 | case XML_ELEMENT_NODE: |
7448 | 0 | case XML_ATTRIBUTE_NODE: |
7449 | 0 | break; |
7450 | 0 | default: |
7451 | 0 | return (1); |
7452 | 0 | } |
7453 | 0 | xmlUnlinkNodeInternal(node); |
7454 | | /* |
7455 | | * Save out-of-scope ns-references in doc->oldNs. |
7456 | | */ |
7457 | 0 | do { |
7458 | 0 | switch (node->type) { |
7459 | 0 | case XML_ELEMENT_NODE: |
7460 | 0 | if ((ctxt == NULL) && (node->nsDef != NULL)) { |
7461 | 0 | ns = node->nsDef; |
7462 | 0 | do { |
7463 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
7464 | 0 | &nbList, ns, ns) == -1) |
7465 | 0 | ret = -1; |
7466 | 0 | ns = ns->next; |
7467 | 0 | } while (ns != NULL); |
7468 | 0 | } |
7469 | | /* Falls through. */ |
7470 | 0 | case XML_ATTRIBUTE_NODE: |
7471 | 0 | if (node->ns != NULL) { |
7472 | | /* |
7473 | | * Find a mapping. |
7474 | | */ |
7475 | 0 | if (list != NULL) { |
7476 | 0 | for (i = 0, j = 0; i < nbList; i++, j += 2) { |
7477 | 0 | if (node->ns == list[j]) { |
7478 | 0 | node->ns = list[++j]; |
7479 | 0 | goto next_node; |
7480 | 0 | } |
7481 | 0 | } |
7482 | 0 | } |
7483 | 0 | ns = NULL; |
7484 | 0 | if (ctxt != NULL) { |
7485 | | /* |
7486 | | * User defined. |
7487 | | */ |
7488 | 0 | } else { |
7489 | | /* |
7490 | | * Add to doc's oldNs. |
7491 | | */ |
7492 | 0 | ns = xmlDOMWrapStoreNs(doc, node->ns->href, |
7493 | 0 | node->ns->prefix); |
7494 | 0 | if (ns == NULL) |
7495 | 0 | ret = -1; |
7496 | 0 | } |
7497 | 0 | if (ns != NULL) { |
7498 | | /* |
7499 | | * Add mapping. |
7500 | | */ |
7501 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&list, &sizeList, |
7502 | 0 | &nbList, node->ns, ns) == -1) |
7503 | 0 | ret = -1; |
7504 | 0 | } |
7505 | 0 | node->ns = ns; |
7506 | 0 | } |
7507 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
7508 | 0 | (node->properties != NULL)) { |
7509 | 0 | node = (xmlNodePtr) node->properties; |
7510 | 0 | continue; |
7511 | 0 | } |
7512 | 0 | break; |
7513 | 0 | default: |
7514 | 0 | goto next_sibling; |
7515 | 0 | } |
7516 | 0 | next_node: |
7517 | 0 | if ((node->type == XML_ELEMENT_NODE) && |
7518 | 0 | (node->children != NULL)) { |
7519 | 0 | node = node->children; |
7520 | 0 | continue; |
7521 | 0 | } |
7522 | 0 | next_sibling: |
7523 | 0 | if (node == NULL) |
7524 | 0 | break; |
7525 | 0 | if (node->next != NULL) |
7526 | 0 | node = node->next; |
7527 | 0 | else { |
7528 | 0 | int type = node->type; |
7529 | |
|
7530 | 0 | node = node->parent; |
7531 | 0 | if ((type == XML_ATTRIBUTE_NODE) && |
7532 | 0 | (node != NULL) && |
7533 | 0 | (node->children != NULL)) { |
7534 | 0 | node = node->children; |
7535 | 0 | } else { |
7536 | 0 | goto next_sibling; |
7537 | 0 | } |
7538 | 0 | } |
7539 | 0 | } while (node != NULL); |
7540 | | |
7541 | 0 | if (list != NULL) |
7542 | 0 | xmlFree(list); |
7543 | 0 | return (ret); |
7544 | 0 | } |
7545 | | |
7546 | | /* |
7547 | | * xmlSearchNsByNamespaceStrict: |
7548 | | * @doc: the document |
7549 | | * @node: the start node |
7550 | | * @nsName: the searched namespace name |
7551 | | * @retNs: the resulting ns-decl |
7552 | | * @prefixed: if the found ns-decl must have a prefix (for attributes) |
7553 | | * |
7554 | | * Dynamically searches for a ns-declaration which matches |
7555 | | * the given @nsName in the ancestor-or-self axis of @node. |
7556 | | * |
7557 | | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
7558 | | * and internal errors. |
7559 | | */ |
7560 | | static int |
7561 | | xmlSearchNsByNamespaceStrict(xmlDocPtr doc, xmlNodePtr node, |
7562 | | const xmlChar* nsName, |
7563 | | xmlNsPtr *retNs, int prefixed) |
7564 | 0 | { |
7565 | 0 | xmlNodePtr cur, prev = NULL, out = NULL; |
7566 | 0 | xmlNsPtr ns, prevns; |
7567 | |
|
7568 | 0 | if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) |
7569 | 0 | return (-1); |
7570 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
7571 | 0 | return(-1); |
7572 | | |
7573 | 0 | *retNs = NULL; |
7574 | 0 | if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { |
7575 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7576 | 0 | if (*retNs == NULL) |
7577 | 0 | return (-1); |
7578 | 0 | return (1); |
7579 | 0 | } |
7580 | 0 | cur = node; |
7581 | 0 | do { |
7582 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7583 | 0 | if (cur->nsDef != NULL) { |
7584 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
7585 | 0 | if (prefixed && (ns->prefix == NULL)) |
7586 | 0 | continue; |
7587 | 0 | if (prev != NULL) { |
7588 | | /* |
7589 | | * Check the last level of ns-decls for a |
7590 | | * shadowing prefix. |
7591 | | */ |
7592 | 0 | prevns = prev->nsDef; |
7593 | 0 | do { |
7594 | 0 | if ((prevns->prefix == ns->prefix) || |
7595 | 0 | ((prevns->prefix != NULL) && |
7596 | 0 | (ns->prefix != NULL) && |
7597 | 0 | xmlStrEqual(prevns->prefix, ns->prefix))) { |
7598 | | /* |
7599 | | * Shadowed. |
7600 | | */ |
7601 | 0 | break; |
7602 | 0 | } |
7603 | 0 | prevns = prevns->next; |
7604 | 0 | } while (prevns != NULL); |
7605 | 0 | if (prevns != NULL) |
7606 | 0 | continue; |
7607 | 0 | } |
7608 | | /* |
7609 | | * Ns-name comparison. |
7610 | | */ |
7611 | 0 | if ((nsName == ns->href) || |
7612 | 0 | xmlStrEqual(nsName, ns->href)) { |
7613 | | /* |
7614 | | * At this point the prefix can only be shadowed, |
7615 | | * if we are the the (at least) 3rd level of |
7616 | | * ns-decls. |
7617 | | */ |
7618 | 0 | if (out) { |
7619 | 0 | int ret; |
7620 | |
|
7621 | 0 | ret = xmlNsInScope(doc, node, prev, ns->prefix); |
7622 | 0 | if (ret < 0) |
7623 | 0 | return (-1); |
7624 | | /* |
7625 | | * TODO: Should we try to find a matching ns-name |
7626 | | * only once? This here keeps on searching. |
7627 | | * I think we should try further since, there might |
7628 | | * be an other matching ns-decl with an unshadowed |
7629 | | * prefix. |
7630 | | */ |
7631 | 0 | if (! ret) |
7632 | 0 | continue; |
7633 | 0 | } |
7634 | 0 | *retNs = ns; |
7635 | 0 | return (1); |
7636 | 0 | } |
7637 | 0 | } |
7638 | 0 | out = prev; |
7639 | 0 | prev = cur; |
7640 | 0 | } |
7641 | 0 | } else if (cur->type == XML_ENTITY_DECL) |
7642 | 0 | return (0); |
7643 | 0 | cur = cur->parent; |
7644 | 0 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
7645 | 0 | return (0); |
7646 | 0 | } |
7647 | | |
7648 | | /* |
7649 | | * xmlSearchNsByPrefixStrict: |
7650 | | * @doc: the document |
7651 | | * @node: the start node |
7652 | | * @prefix: the searched namespace prefix |
7653 | | * @retNs: the resulting ns-decl |
7654 | | * |
7655 | | * Dynamically searches for a ns-declaration which matches |
7656 | | * the given @nsName in the ancestor-or-self axis of @node. |
7657 | | * |
7658 | | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
7659 | | * and internal errors. |
7660 | | */ |
7661 | | static int |
7662 | | xmlSearchNsByPrefixStrict(xmlDocPtr doc, xmlNodePtr node, |
7663 | | const xmlChar* prefix, |
7664 | | xmlNsPtr *retNs) |
7665 | 0 | { |
7666 | 0 | xmlNodePtr cur; |
7667 | 0 | xmlNsPtr ns; |
7668 | |
|
7669 | 0 | if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
7670 | 0 | return(-1); |
7671 | | |
7672 | 0 | if (retNs) |
7673 | 0 | *retNs = NULL; |
7674 | 0 | if (IS_STR_XML(prefix)) { |
7675 | 0 | if (retNs) { |
7676 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7677 | 0 | if (*retNs == NULL) |
7678 | 0 | return (-1); |
7679 | 0 | } |
7680 | 0 | return (1); |
7681 | 0 | } |
7682 | 0 | cur = node; |
7683 | 0 | do { |
7684 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
7685 | 0 | if (cur->nsDef != NULL) { |
7686 | 0 | ns = cur->nsDef; |
7687 | 0 | do { |
7688 | 0 | if ((prefix == ns->prefix) || |
7689 | 0 | xmlStrEqual(prefix, ns->prefix)) |
7690 | 0 | { |
7691 | | /* |
7692 | | * Disabled namespaces, e.g. xmlns:abc="". |
7693 | | */ |
7694 | 0 | if (ns->href == NULL) |
7695 | 0 | return(0); |
7696 | 0 | if (retNs) |
7697 | 0 | *retNs = ns; |
7698 | 0 | return (1); |
7699 | 0 | } |
7700 | 0 | ns = ns->next; |
7701 | 0 | } while (ns != NULL); |
7702 | 0 | } |
7703 | 0 | } else if (cur->type == XML_ENTITY_DECL) |
7704 | 0 | return (0); |
7705 | 0 | cur = cur->parent; |
7706 | 0 | } while ((cur != NULL) && (cur->doc != (xmlDocPtr) cur)); |
7707 | 0 | return (0); |
7708 | 0 | } |
7709 | | |
7710 | | /* |
7711 | | * xmlDOMWrapNSNormDeclareNsForced: |
7712 | | * @doc: the doc |
7713 | | * @elem: the element-node to declare on |
7714 | | * @nsName: the namespace-name of the ns-decl |
7715 | | * @prefix: the preferred prefix of the ns-decl |
7716 | | * @checkShadow: ensure that the new ns-decl doesn't shadow ancestor ns-decls |
7717 | | * |
7718 | | * Declares a new namespace on @elem. It tries to use the |
7719 | | * given @prefix; if a ns-decl with the given prefix is already existent |
7720 | | * on @elem, it will generate an other prefix. |
7721 | | * |
7722 | | * Returns 1 if a ns-decl was found, 0 if not and -1 on API |
7723 | | * and internal errors. |
7724 | | */ |
7725 | | static xmlNsPtr |
7726 | | xmlDOMWrapNSNormDeclareNsForced(xmlDocPtr doc, |
7727 | | xmlNodePtr elem, |
7728 | | const xmlChar *nsName, |
7729 | | const xmlChar *prefix, |
7730 | | int checkShadow) |
7731 | 0 | { |
7732 | |
|
7733 | 0 | xmlNsPtr ret; |
7734 | 0 | char buf[50]; |
7735 | 0 | const xmlChar *pref; |
7736 | 0 | int counter = 0; |
7737 | |
|
7738 | 0 | if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE)) |
7739 | 0 | return(NULL); |
7740 | | /* |
7741 | | * Create a ns-decl on @anchor. |
7742 | | */ |
7743 | 0 | pref = prefix; |
7744 | 0 | while (1) { |
7745 | | /* |
7746 | | * Lookup whether the prefix is unused in elem's ns-decls. |
7747 | | */ |
7748 | 0 | if ((elem->nsDef != NULL) && |
7749 | 0 | (xmlTreeNSListLookupByPrefix(elem->nsDef, pref) != NULL)) |
7750 | 0 | goto ns_next_prefix; |
7751 | 0 | if (checkShadow && elem->parent && |
7752 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
7753 | | /* |
7754 | | * Does it shadow ancestor ns-decls? |
7755 | | */ |
7756 | 0 | if (xmlSearchNsByPrefixStrict(doc, elem->parent, pref, NULL) == 1) |
7757 | 0 | goto ns_next_prefix; |
7758 | 0 | } |
7759 | 0 | ret = xmlNewNs(NULL, nsName, pref); |
7760 | 0 | if (ret == NULL) |
7761 | 0 | return (NULL); |
7762 | 0 | if (elem->nsDef == NULL) |
7763 | 0 | elem->nsDef = ret; |
7764 | 0 | else { |
7765 | 0 | xmlNsPtr ns2 = elem->nsDef; |
7766 | 0 | while (ns2->next != NULL) |
7767 | 0 | ns2 = ns2->next; |
7768 | 0 | ns2->next = ret; |
7769 | 0 | } |
7770 | 0 | return (ret); |
7771 | 0 | ns_next_prefix: |
7772 | 0 | counter++; |
7773 | 0 | if (counter > 1000) |
7774 | 0 | return (NULL); |
7775 | 0 | if (prefix == NULL) { |
7776 | 0 | snprintf((char *) buf, sizeof(buf), |
7777 | 0 | "ns_%d", counter); |
7778 | 0 | } else |
7779 | 0 | snprintf((char *) buf, sizeof(buf), |
7780 | 0 | "%.30s_%d", (char *)prefix, counter); |
7781 | 0 | pref = BAD_CAST buf; |
7782 | 0 | } |
7783 | 0 | } |
7784 | | |
7785 | | /* |
7786 | | * xmlDOMWrapNSNormAcquireNormalizedNs: |
7787 | | * @doc: the doc |
7788 | | * @elem: the element-node to declare namespaces on |
7789 | | * @ns: the ns-struct to use for the search |
7790 | | * @retNs: the found/created ns-struct |
7791 | | * @nsMap: the ns-map |
7792 | | * @depth: the current tree depth |
7793 | | * @ancestorsOnly: search in ancestor ns-decls only |
7794 | | * @prefixed: if the searched ns-decl must have a prefix (for attributes) |
7795 | | * |
7796 | | * Searches for a matching ns-name in the ns-decls of @nsMap, if not |
7797 | | * found it will either declare it on @elem, or store it in doc->oldNs. |
7798 | | * If a new ns-decl needs to be declared on @elem, it tries to use the |
7799 | | * @ns->prefix for it, if this prefix is already in use on @elem, it will |
7800 | | * change the prefix or the new ns-decl. |
7801 | | * |
7802 | | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
7803 | | */ |
7804 | | static int |
7805 | | xmlDOMWrapNSNormAcquireNormalizedNs(xmlDocPtr doc, |
7806 | | xmlNodePtr elem, |
7807 | | xmlNsPtr ns, |
7808 | | xmlNsPtr *retNs, |
7809 | | xmlNsMapPtr *nsMap, |
7810 | | |
7811 | | int depth, |
7812 | | int ancestorsOnly, |
7813 | | int prefixed) |
7814 | 0 | { |
7815 | 0 | xmlNsMapItemPtr mi; |
7816 | |
|
7817 | 0 | if ((doc == NULL) || (ns == NULL) || (retNs == NULL) || |
7818 | 0 | (nsMap == NULL)) |
7819 | 0 | return (-1); |
7820 | | |
7821 | 0 | *retNs = NULL; |
7822 | | /* |
7823 | | * Handle XML namespace. |
7824 | | */ |
7825 | 0 | if (IS_STR_XML(ns->prefix)) { |
7826 | | /* |
7827 | | * Insert XML namespace mapping. |
7828 | | */ |
7829 | 0 | *retNs = xmlTreeEnsureXMLDecl(doc); |
7830 | 0 | if (*retNs == NULL) |
7831 | 0 | return (-1); |
7832 | 0 | return (0); |
7833 | 0 | } |
7834 | | /* |
7835 | | * If the search should be done in ancestors only and no |
7836 | | * @elem (the first ancestor) was specified, then skip the search. |
7837 | | */ |
7838 | 0 | if ((XML_NSMAP_NOTEMPTY(*nsMap)) && |
7839 | 0 | (! (ancestorsOnly && (elem == NULL)))) |
7840 | 0 | { |
7841 | | /* |
7842 | | * Try to find an equal ns-name in in-scope ns-decls. |
7843 | | */ |
7844 | 0 | XML_NSMAP_FOREACH(*nsMap, mi) { |
7845 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
7846 | | /* |
7847 | | * ancestorsOnly: This should be turned on to gain speed, |
7848 | | * if one knows that the branch itself was already |
7849 | | * ns-wellformed and no stale references existed. |
7850 | | * I.e. it searches in the ancestor axis only. |
7851 | | */ |
7852 | 0 | ((! ancestorsOnly) || (mi->depth == XML_TREE_NSMAP_PARENT)) && |
7853 | | /* Skip shadowed prefixes. */ |
7854 | 0 | (mi->shadowDepth == -1) && |
7855 | | /* Skip xmlns="" or xmlns:foo="". */ |
7856 | 0 | ((mi->newNs->href != NULL) && |
7857 | 0 | (mi->newNs->href[0] != 0)) && |
7858 | | /* Ensure a prefix if wanted. */ |
7859 | 0 | ((! prefixed) || (mi->newNs->prefix != NULL)) && |
7860 | | /* Equal ns name */ |
7861 | 0 | ((mi->newNs->href == ns->href) || |
7862 | 0 | xmlStrEqual(mi->newNs->href, ns->href))) { |
7863 | | /* Set the mapping. */ |
7864 | 0 | mi->oldNs = ns; |
7865 | 0 | *retNs = mi->newNs; |
7866 | 0 | return (0); |
7867 | 0 | } |
7868 | 0 | } |
7869 | 0 | } |
7870 | | /* |
7871 | | * No luck, the namespace is out of scope or shadowed. |
7872 | | */ |
7873 | 0 | if (elem == NULL) { |
7874 | 0 | xmlNsPtr tmpns; |
7875 | | |
7876 | | /* |
7877 | | * Store ns-decls in "oldNs" of the document-node. |
7878 | | */ |
7879 | 0 | tmpns = xmlDOMWrapStoreNs(doc, ns->href, ns->prefix); |
7880 | 0 | if (tmpns == NULL) |
7881 | 0 | return (-1); |
7882 | | /* |
7883 | | * Insert mapping. |
7884 | | */ |
7885 | 0 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, |
7886 | 0 | tmpns, XML_TREE_NSMAP_DOC) == NULL) { |
7887 | 0 | return (-1); |
7888 | 0 | } |
7889 | 0 | *retNs = tmpns; |
7890 | 0 | } else { |
7891 | 0 | xmlNsPtr tmpns; |
7892 | |
|
7893 | 0 | tmpns = xmlDOMWrapNSNormDeclareNsForced(doc, elem, ns->href, |
7894 | 0 | ns->prefix, 0); |
7895 | 0 | if (tmpns == NULL) |
7896 | 0 | return (-1); |
7897 | | |
7898 | 0 | if (*nsMap != NULL) { |
7899 | | /* |
7900 | | * Does it shadow ancestor ns-decls? |
7901 | | */ |
7902 | 0 | XML_NSMAP_FOREACH(*nsMap, mi) { |
7903 | 0 | if ((mi->depth < depth) && |
7904 | 0 | (mi->shadowDepth == -1) && |
7905 | 0 | ((ns->prefix == mi->newNs->prefix) || |
7906 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
7907 | | /* |
7908 | | * Shadows. |
7909 | | */ |
7910 | 0 | mi->shadowDepth = depth; |
7911 | 0 | break; |
7912 | 0 | } |
7913 | 0 | } |
7914 | 0 | } |
7915 | 0 | if (xmlDOMWrapNsMapAddItem(nsMap, -1, ns, tmpns, depth) == NULL) { |
7916 | 0 | return (-1); |
7917 | 0 | } |
7918 | 0 | *retNs = tmpns; |
7919 | 0 | } |
7920 | 0 | return (0); |
7921 | 0 | } |
7922 | | |
7923 | | typedef enum { |
7924 | | XML_DOM_RECONNS_REMOVEREDUND = 1<<0 |
7925 | | } xmlDOMReconcileNSOptions; |
7926 | | |
7927 | | /* |
7928 | | * xmlDOMWrapReconcileNamespaces: |
7929 | | * @ctxt: DOM wrapper context, unused at the moment |
7930 | | * @elem: the element-node |
7931 | | * @options: option flags |
7932 | | * |
7933 | | * Ensures that ns-references point to ns-decls hold on element-nodes. |
7934 | | * Ensures that the tree is namespace wellformed by creating additional |
7935 | | * ns-decls where needed. Note that, since prefixes of already existent |
7936 | | * ns-decls can be shadowed by this process, it could break QNames in |
7937 | | * attribute values or element content. |
7938 | | * |
7939 | | * NOTE: This function was not intensively tested. |
7940 | | * |
7941 | | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
7942 | | */ |
7943 | | |
7944 | | int |
7945 | | xmlDOMWrapReconcileNamespaces(xmlDOMWrapCtxtPtr ctxt ATTRIBUTE_UNUSED, |
7946 | | xmlNodePtr elem, |
7947 | | int options) |
7948 | 0 | { |
7949 | 0 | int depth = -1, adoptns = 0, parnsdone = 0; |
7950 | 0 | xmlNsPtr ns, prevns; |
7951 | 0 | xmlDocPtr doc; |
7952 | 0 | xmlNodePtr cur, curElem = NULL; |
7953 | 0 | xmlNsMapPtr nsMap = NULL; |
7954 | 0 | xmlNsMapItemPtr /* topmi = NULL, */ mi; |
7955 | | /* @ancestorsOnly should be set by an option flag. */ |
7956 | 0 | int ancestorsOnly = 0; |
7957 | 0 | int optRemoveRedundantNS = |
7958 | 0 | ((xmlDOMReconcileNSOptions) options & XML_DOM_RECONNS_REMOVEREDUND) ? 1 : 0; |
7959 | 0 | xmlNsPtr *listRedund = NULL; |
7960 | 0 | int sizeRedund = 0, nbRedund = 0, ret = 0, i, j; |
7961 | |
|
7962 | 0 | if ((elem == NULL) || (elem->doc == NULL) || |
7963 | 0 | (elem->type != XML_ELEMENT_NODE)) |
7964 | 0 | return (-1); |
7965 | | |
7966 | 0 | doc = elem->doc; |
7967 | 0 | cur = elem; |
7968 | 0 | do { |
7969 | 0 | switch (cur->type) { |
7970 | 0 | case XML_ELEMENT_NODE: |
7971 | 0 | adoptns = 1; |
7972 | 0 | curElem = cur; |
7973 | 0 | depth++; |
7974 | | /* |
7975 | | * Namespace declarations. |
7976 | | */ |
7977 | 0 | if (cur->nsDef != NULL) { |
7978 | 0 | prevns = NULL; |
7979 | 0 | ns = cur->nsDef; |
7980 | 0 | while (ns != NULL) { |
7981 | 0 | if (! parnsdone) { |
7982 | 0 | if ((elem->parent) && |
7983 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
7984 | | /* |
7985 | | * Gather ancestor in-scope ns-decls. |
7986 | | */ |
7987 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
7988 | 0 | elem->parent) == -1) |
7989 | 0 | ret = -1; |
7990 | 0 | } |
7991 | 0 | parnsdone = 1; |
7992 | 0 | } |
7993 | | |
7994 | | /* |
7995 | | * Lookup the ns ancestor-axis for equal ns-decls in scope. |
7996 | | */ |
7997 | 0 | if (optRemoveRedundantNS && XML_NSMAP_NOTEMPTY(nsMap)) { |
7998 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
7999 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
8000 | 0 | (mi->shadowDepth == -1) && |
8001 | 0 | ((ns->prefix == mi->newNs->prefix) || |
8002 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix)) && |
8003 | 0 | ((ns->href == mi->newNs->href) || |
8004 | 0 | xmlStrEqual(ns->href, mi->newNs->href))) |
8005 | 0 | { |
8006 | | /* |
8007 | | * A redundant ns-decl was found. |
8008 | | * Add it to the list of redundant ns-decls. |
8009 | | */ |
8010 | 0 | if (xmlDOMWrapNSNormAddNsMapItem2(&listRedund, |
8011 | 0 | &sizeRedund, &nbRedund, ns, mi->newNs) == -1) { |
8012 | 0 | ret = -1; |
8013 | 0 | } else { |
8014 | | /* |
8015 | | * Remove the ns-decl from the element-node. |
8016 | | */ |
8017 | 0 | if (prevns) |
8018 | 0 | prevns->next = ns->next; |
8019 | 0 | else |
8020 | 0 | cur->nsDef = ns->next; |
8021 | 0 | goto next_ns_decl; |
8022 | 0 | } |
8023 | 0 | } |
8024 | 0 | } |
8025 | 0 | } |
8026 | | |
8027 | | /* |
8028 | | * Skip ns-references handling if the referenced |
8029 | | * ns-decl is declared on the same element. |
8030 | | */ |
8031 | 0 | if ((cur->ns != NULL) && adoptns && (cur->ns == ns)) |
8032 | 0 | adoptns = 0; |
8033 | | /* |
8034 | | * Does it shadow any ns-decl? |
8035 | | */ |
8036 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8037 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8038 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
8039 | 0 | (mi->shadowDepth == -1) && |
8040 | 0 | ((ns->prefix == mi->newNs->prefix) || |
8041 | 0 | xmlStrEqual(ns->prefix, mi->newNs->prefix))) { |
8042 | |
|
8043 | 0 | mi->shadowDepth = depth; |
8044 | 0 | } |
8045 | 0 | } |
8046 | 0 | } |
8047 | | /* |
8048 | | * Push mapping. |
8049 | | */ |
8050 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, ns, ns, |
8051 | 0 | depth) == NULL) |
8052 | 0 | ret = -1; |
8053 | |
|
8054 | 0 | prevns = ns; |
8055 | 0 | next_ns_decl: |
8056 | 0 | ns = ns->next; |
8057 | 0 | } |
8058 | 0 | } |
8059 | 0 | if (! adoptns) |
8060 | 0 | goto ns_end; |
8061 | | /* Falls through. */ |
8062 | 0 | case XML_ATTRIBUTE_NODE: |
8063 | | /* No ns, no fun. */ |
8064 | 0 | if (cur->ns == NULL) |
8065 | 0 | goto ns_end; |
8066 | | |
8067 | 0 | if (! parnsdone) { |
8068 | 0 | if ((elem->parent) && |
8069 | 0 | ((xmlNodePtr) elem->parent->doc != elem->parent)) { |
8070 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
8071 | 0 | elem->parent) == -1) |
8072 | 0 | ret = -1; |
8073 | 0 | } |
8074 | 0 | parnsdone = 1; |
8075 | 0 | } |
8076 | | /* |
8077 | | * Adjust the reference if this was a redundant ns-decl. |
8078 | | */ |
8079 | 0 | if (listRedund) { |
8080 | 0 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
8081 | 0 | if (cur->ns == listRedund[j]) { |
8082 | 0 | cur->ns = listRedund[++j]; |
8083 | 0 | break; |
8084 | 0 | } |
8085 | 0 | } |
8086 | 0 | } |
8087 | | /* |
8088 | | * Adopt ns-references. |
8089 | | */ |
8090 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8091 | | /* |
8092 | | * Search for a mapping. |
8093 | | */ |
8094 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8095 | 0 | if ((mi->shadowDepth == -1) && |
8096 | 0 | (cur->ns == mi->oldNs)) { |
8097 | |
|
8098 | 0 | cur->ns = mi->newNs; |
8099 | 0 | goto ns_end; |
8100 | 0 | } |
8101 | 0 | } |
8102 | 0 | } |
8103 | | /* |
8104 | | * Acquire a normalized ns-decl and add it to the map. |
8105 | | */ |
8106 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(doc, curElem, |
8107 | 0 | cur->ns, &ns, |
8108 | 0 | &nsMap, depth, |
8109 | 0 | ancestorsOnly, |
8110 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
8111 | 0 | ret = -1; |
8112 | 0 | cur->ns = ns; |
8113 | |
|
8114 | 0 | ns_end: |
8115 | 0 | if ((cur->type == XML_ELEMENT_NODE) && |
8116 | 0 | (cur->properties != NULL)) { |
8117 | | /* |
8118 | | * Process attributes. |
8119 | | */ |
8120 | 0 | cur = (xmlNodePtr) cur->properties; |
8121 | 0 | continue; |
8122 | 0 | } |
8123 | 0 | break; |
8124 | 0 | default: |
8125 | 0 | goto next_sibling; |
8126 | 0 | } |
8127 | 0 | into_content: |
8128 | 0 | if ((cur->type == XML_ELEMENT_NODE) && |
8129 | 0 | (cur->children != NULL)) { |
8130 | | /* |
8131 | | * Process content of element-nodes only. |
8132 | | */ |
8133 | 0 | cur = cur->children; |
8134 | 0 | continue; |
8135 | 0 | } |
8136 | 0 | next_sibling: |
8137 | 0 | if (cur == elem) |
8138 | 0 | break; |
8139 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
8140 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8141 | | /* |
8142 | | * Pop mappings. |
8143 | | */ |
8144 | 0 | while ((nsMap->last != NULL) && |
8145 | 0 | (nsMap->last->depth >= depth)) |
8146 | 0 | { |
8147 | 0 | XML_NSMAP_POP(nsMap, mi) |
8148 | 0 | } |
8149 | | /* |
8150 | | * Unshadow. |
8151 | | */ |
8152 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8153 | 0 | if (mi->shadowDepth >= depth) |
8154 | 0 | mi->shadowDepth = -1; |
8155 | 0 | } |
8156 | 0 | } |
8157 | 0 | depth--; |
8158 | 0 | } |
8159 | 0 | if (cur->next != NULL) |
8160 | 0 | cur = cur->next; |
8161 | 0 | else { |
8162 | 0 | if (cur->type == XML_ATTRIBUTE_NODE) { |
8163 | 0 | cur = cur->parent; |
8164 | 0 | goto into_content; |
8165 | 0 | } |
8166 | 0 | cur = cur->parent; |
8167 | 0 | goto next_sibling; |
8168 | 0 | } |
8169 | 0 | } while (cur != NULL); |
8170 | | |
8171 | 0 | if (listRedund) { |
8172 | 0 | for (i = 0, j = 0; i < nbRedund; i++, j += 2) { |
8173 | 0 | xmlFreeNs(listRedund[j]); |
8174 | 0 | } |
8175 | 0 | xmlFree(listRedund); |
8176 | 0 | } |
8177 | 0 | if (nsMap != NULL) |
8178 | 0 | xmlDOMWrapNsMapFree(nsMap); |
8179 | 0 | return (ret); |
8180 | 0 | } |
8181 | | |
8182 | | /* |
8183 | | * xmlDOMWrapAdoptBranch: |
8184 | | * @ctxt: the optional context for custom processing |
8185 | | * @sourceDoc: the optional sourceDoc |
8186 | | * @node: the element-node to start with |
8187 | | * @destDoc: the destination doc for adoption |
8188 | | * @destParent: the optional new parent of @node in @destDoc |
8189 | | * @options: option flags |
8190 | | * |
8191 | | * Ensures that ns-references point to @destDoc: either to |
8192 | | * elements->nsDef entries if @destParent is given, or to |
8193 | | * @destDoc->oldNs otherwise. |
8194 | | * If @destParent is given, it ensures that the tree is namespace |
8195 | | * wellformed by creating additional ns-decls where needed. |
8196 | | * Note that, since prefixes of already existent ns-decls can be |
8197 | | * shadowed by this process, it could break QNames in attribute |
8198 | | * values or element content. |
8199 | | * |
8200 | | * NOTE: This function was not intensively tested. |
8201 | | * |
8202 | | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
8203 | | */ |
8204 | | static int |
8205 | | xmlDOMWrapAdoptBranch(xmlDOMWrapCtxtPtr ctxt, |
8206 | | xmlDocPtr sourceDoc ATTRIBUTE_UNUSED, |
8207 | | xmlNodePtr node, |
8208 | | xmlDocPtr destDoc, |
8209 | | xmlNodePtr destParent, |
8210 | | int options ATTRIBUTE_UNUSED) |
8211 | 0 | { |
8212 | 0 | int ret = 0; |
8213 | 0 | xmlNodePtr cur, curElem = NULL; |
8214 | 0 | xmlNsMapPtr nsMap = NULL; |
8215 | 0 | xmlNsMapItemPtr mi; |
8216 | 0 | xmlNsPtr ns = NULL; |
8217 | 0 | int depth = -1; |
8218 | | /* gather @parent's ns-decls. */ |
8219 | 0 | int parnsdone; |
8220 | | /* @ancestorsOnly should be set per option. */ |
8221 | 0 | int ancestorsOnly = 0; |
8222 | | |
8223 | | /* |
8224 | | * Get the ns-map from the context if available. |
8225 | | */ |
8226 | 0 | if (ctxt) |
8227 | 0 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
8228 | | /* |
8229 | | * Disable search for ns-decls in the parent-axis of the |
8230 | | * destination element, if: |
8231 | | * 1) there's no destination parent |
8232 | | * 2) custom ns-reference handling is used |
8233 | | */ |
8234 | 0 | if ((destParent == NULL) || |
8235 | 0 | (ctxt && ctxt->getNsForNodeFunc)) |
8236 | 0 | { |
8237 | 0 | parnsdone = 1; |
8238 | 0 | } else |
8239 | 0 | parnsdone = 0; |
8240 | |
|
8241 | 0 | cur = node; |
8242 | |
|
8243 | 0 | while (cur != NULL) { |
8244 | 0 | if (cur->doc != destDoc) { |
8245 | 0 | if (xmlNodeSetDoc(cur, destDoc) < 0) |
8246 | 0 | ret = -1; |
8247 | 0 | } |
8248 | |
|
8249 | 0 | switch (cur->type) { |
8250 | 0 | case XML_XINCLUDE_START: |
8251 | 0 | case XML_XINCLUDE_END: |
8252 | | /* |
8253 | | * TODO |
8254 | | */ |
8255 | 0 | ret = -1; |
8256 | 0 | goto leave_node; |
8257 | 0 | case XML_ELEMENT_NODE: |
8258 | 0 | curElem = cur; |
8259 | 0 | depth++; |
8260 | | /* |
8261 | | * Namespace declarations. |
8262 | | * - ns->href and ns->prefix are never in the dict, so |
8263 | | * we need not move the values over to the destination dict. |
8264 | | * - Note that for custom handling of ns-references, |
8265 | | * the ns-decls need not be stored in the ns-map, |
8266 | | * since they won't be referenced by node->ns. |
8267 | | */ |
8268 | 0 | if ((cur->nsDef) && |
8269 | 0 | ((ctxt == NULL) || (ctxt->getNsForNodeFunc == NULL))) |
8270 | 0 | { |
8271 | 0 | if (! parnsdone) { |
8272 | | /* |
8273 | | * Gather @parent's in-scope ns-decls. |
8274 | | */ |
8275 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
8276 | 0 | destParent) == -1) |
8277 | 0 | ret = -1; |
8278 | 0 | parnsdone = 1; |
8279 | 0 | } |
8280 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
8281 | | /* |
8282 | | * NOTE: ns->prefix and ns->href are never in the dict. |
8283 | | */ |
8284 | | /* |
8285 | | * Does it shadow any ns-decl? |
8286 | | */ |
8287 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8288 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8289 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
8290 | 0 | (mi->shadowDepth == -1) && |
8291 | 0 | ((ns->prefix == mi->newNs->prefix) || |
8292 | 0 | xmlStrEqual(ns->prefix, |
8293 | 0 | mi->newNs->prefix))) { |
8294 | |
|
8295 | 0 | mi->shadowDepth = depth; |
8296 | 0 | } |
8297 | 0 | } |
8298 | 0 | } |
8299 | | /* |
8300 | | * Push mapping. |
8301 | | */ |
8302 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8303 | 0 | ns, ns, depth) == NULL) |
8304 | 0 | ret = -1; |
8305 | 0 | } |
8306 | 0 | } |
8307 | | /* Falls through. */ |
8308 | 0 | case XML_ATTRIBUTE_NODE: |
8309 | | /* No namespace, no fun. */ |
8310 | 0 | if (cur->ns == NULL) |
8311 | 0 | goto ns_end; |
8312 | | |
8313 | 0 | if (! parnsdone) { |
8314 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
8315 | 0 | destParent) == -1) |
8316 | 0 | ret = -1; |
8317 | 0 | parnsdone = 1; |
8318 | 0 | } |
8319 | | /* |
8320 | | * Adopt ns-references. |
8321 | | */ |
8322 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8323 | | /* |
8324 | | * Search for a mapping. |
8325 | | */ |
8326 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8327 | 0 | if ((mi->shadowDepth == -1) && |
8328 | 0 | (cur->ns == mi->oldNs)) { |
8329 | |
|
8330 | 0 | cur->ns = mi->newNs; |
8331 | 0 | goto ns_end; |
8332 | 0 | } |
8333 | 0 | } |
8334 | 0 | } |
8335 | | /* |
8336 | | * No matching namespace in scope. We need a new one. |
8337 | | */ |
8338 | 0 | if ((ctxt) && (ctxt->getNsForNodeFunc)) { |
8339 | | /* |
8340 | | * User-defined behaviour. |
8341 | | */ |
8342 | 0 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
8343 | 0 | cur->ns->href, cur->ns->prefix); |
8344 | | /* |
8345 | | * Insert mapping if ns is available; it's the users fault |
8346 | | * if not. |
8347 | | */ |
8348 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8349 | 0 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
8350 | 0 | ret = -1; |
8351 | 0 | cur->ns = ns; |
8352 | 0 | } else { |
8353 | | /* |
8354 | | * Acquire a normalized ns-decl and add it to the map. |
8355 | | */ |
8356 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc, |
8357 | | /* ns-decls on curElem or on destDoc->oldNs */ |
8358 | 0 | destParent ? curElem : NULL, |
8359 | 0 | cur->ns, &ns, |
8360 | 0 | &nsMap, depth, |
8361 | 0 | ancestorsOnly, |
8362 | | /* ns-decls must be prefixed for attributes. */ |
8363 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
8364 | 0 | ret = -1; |
8365 | 0 | cur->ns = ns; |
8366 | 0 | } |
8367 | |
|
8368 | 0 | ns_end: |
8369 | 0 | if (cur->type == XML_ELEMENT_NODE) { |
8370 | 0 | cur->psvi = NULL; |
8371 | 0 | cur->line = 0; |
8372 | 0 | cur->extra = 0; |
8373 | | /* |
8374 | | * Walk attributes. |
8375 | | */ |
8376 | 0 | if (cur->properties != NULL) { |
8377 | | /* |
8378 | | * Process first attribute node. |
8379 | | */ |
8380 | 0 | cur = (xmlNodePtr) cur->properties; |
8381 | 0 | continue; |
8382 | 0 | } |
8383 | 0 | } |
8384 | 0 | break; |
8385 | 0 | case XML_TEXT_NODE: |
8386 | 0 | case XML_CDATA_SECTION_NODE: |
8387 | 0 | case XML_PI_NODE: |
8388 | 0 | case XML_COMMENT_NODE: |
8389 | 0 | case XML_ENTITY_REF_NODE: |
8390 | 0 | goto leave_node; |
8391 | 0 | default: |
8392 | 0 | ret = -1; |
8393 | 0 | } |
8394 | | /* |
8395 | | * Walk the tree. |
8396 | | */ |
8397 | 0 | if (cur->children != NULL) { |
8398 | 0 | cur = cur->children; |
8399 | 0 | continue; |
8400 | 0 | } |
8401 | | |
8402 | 0 | leave_node: |
8403 | 0 | if (cur == node) |
8404 | 0 | break; |
8405 | 0 | if ((cur->type == XML_ELEMENT_NODE) || |
8406 | 0 | (cur->type == XML_XINCLUDE_START) || |
8407 | 0 | (cur->type == XML_XINCLUDE_END)) |
8408 | 0 | { |
8409 | | /* |
8410 | | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
8411 | | */ |
8412 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8413 | | /* |
8414 | | * Pop mappings. |
8415 | | */ |
8416 | 0 | while ((nsMap->last != NULL) && |
8417 | 0 | (nsMap->last->depth >= depth)) |
8418 | 0 | { |
8419 | 0 | XML_NSMAP_POP(nsMap, mi) |
8420 | 0 | } |
8421 | | /* |
8422 | | * Unshadow. |
8423 | | */ |
8424 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8425 | 0 | if (mi->shadowDepth >= depth) |
8426 | 0 | mi->shadowDepth = -1; |
8427 | 0 | } |
8428 | 0 | } |
8429 | 0 | depth--; |
8430 | 0 | } |
8431 | 0 | if (cur->next != NULL) |
8432 | 0 | cur = cur->next; |
8433 | 0 | else if ((cur->type == XML_ATTRIBUTE_NODE) && |
8434 | 0 | (cur->parent->children != NULL)) |
8435 | 0 | { |
8436 | 0 | cur = cur->parent->children; |
8437 | 0 | } else { |
8438 | 0 | cur = cur->parent; |
8439 | 0 | goto leave_node; |
8440 | 0 | } |
8441 | 0 | } |
8442 | | |
8443 | | /* |
8444 | | * Cleanup. |
8445 | | */ |
8446 | 0 | if (nsMap != NULL) { |
8447 | 0 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
8448 | | /* |
8449 | | * Just cleanup the map but don't free. |
8450 | | */ |
8451 | 0 | if (nsMap->first) { |
8452 | 0 | if (nsMap->pool) |
8453 | 0 | nsMap->last->next = nsMap->pool; |
8454 | 0 | nsMap->pool = nsMap->first; |
8455 | 0 | nsMap->first = NULL; |
8456 | 0 | } |
8457 | 0 | } else |
8458 | 0 | xmlDOMWrapNsMapFree(nsMap); |
8459 | 0 | } |
8460 | 0 | return(ret); |
8461 | 0 | } |
8462 | | |
8463 | | /* |
8464 | | * xmlDOMWrapCloneNode: |
8465 | | * @ctxt: the optional context for custom processing |
8466 | | * @sourceDoc: the optional sourceDoc |
8467 | | * @node: the node to start with |
8468 | | * @resNode: the clone of the given @node |
8469 | | * @destDoc: the destination doc |
8470 | | * @destParent: the optional new parent of @node in @destDoc |
8471 | | * @deep: descend into child if set |
8472 | | * @options: option flags |
8473 | | * |
8474 | | * References of out-of scope ns-decls are remapped to point to @destDoc: |
8475 | | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
8476 | | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used. |
8477 | | * This is the case when you don't know already where the cloned branch |
8478 | | * will be added to. |
8479 | | * |
8480 | | * If @destParent is given, it ensures that the tree is namespace |
8481 | | * wellformed by creating additional ns-decls where needed. |
8482 | | * Note that, since prefixes of already existent ns-decls can be |
8483 | | * shadowed by this process, it could break QNames in attribute |
8484 | | * values or element content. |
8485 | | * TODO: |
8486 | | * 1) What to do with XInclude? Currently this returns an error for XInclude. |
8487 | | * |
8488 | | * Returns 0 if the operation succeeded, |
8489 | | * 1 if a node of unsupported (or not yet supported) type was given, |
8490 | | * -1 on API/internal errors. |
8491 | | */ |
8492 | | |
8493 | | int |
8494 | | xmlDOMWrapCloneNode(xmlDOMWrapCtxtPtr ctxt, |
8495 | | xmlDocPtr sourceDoc, |
8496 | | xmlNodePtr node, |
8497 | | xmlNodePtr *resNode, |
8498 | | xmlDocPtr destDoc, |
8499 | | xmlNodePtr destParent, |
8500 | | int deep, |
8501 | | int options ATTRIBUTE_UNUSED) |
8502 | 0 | { |
8503 | 0 | int ret = 0; |
8504 | 0 | xmlNodePtr cur, cloneElem = NULL; |
8505 | 0 | xmlNsMapPtr nsMap = NULL; |
8506 | 0 | xmlNsMapItemPtr mi; |
8507 | 0 | xmlNsPtr ns; |
8508 | 0 | int depth = -1; |
8509 | | /* int adoptStr = 1; */ |
8510 | | /* gather @parent's ns-decls. */ |
8511 | 0 | int parnsdone = 0; |
8512 | | /* |
8513 | | * @ancestorsOnly: |
8514 | | * TODO: @ancestorsOnly should be set per option. |
8515 | | * |
8516 | | */ |
8517 | 0 | int ancestorsOnly = 0; |
8518 | 0 | xmlNodePtr resultClone = NULL, clone = NULL, parentClone = NULL, prevClone = NULL; |
8519 | 0 | xmlNsPtr cloneNs = NULL, *cloneNsDefSlot = NULL; |
8520 | 0 | xmlDictPtr dict; /* The destination dict */ |
8521 | |
|
8522 | 0 | if ((node == NULL) || (resNode == NULL) || (destDoc == NULL) || |
8523 | 0 | ((destParent != NULL) && (destParent->doc != destDoc))) |
8524 | 0 | return(-1); |
8525 | | /* |
8526 | | * TODO: Initially we support only element-nodes. |
8527 | | */ |
8528 | 0 | if (node->type != XML_ELEMENT_NODE) |
8529 | 0 | return(1); |
8530 | | /* |
8531 | | * Check node->doc sanity. |
8532 | | */ |
8533 | 0 | if ((node->doc != NULL) && (sourceDoc != NULL) && |
8534 | 0 | (node->doc != sourceDoc)) { |
8535 | | /* |
8536 | | * Might be an XIncluded node. |
8537 | | */ |
8538 | 0 | return (-1); |
8539 | 0 | } |
8540 | 0 | if (sourceDoc == NULL) |
8541 | 0 | sourceDoc = node->doc; |
8542 | 0 | if (sourceDoc == NULL) |
8543 | 0 | return (-1); |
8544 | | |
8545 | 0 | dict = destDoc->dict; |
8546 | | /* |
8547 | | * Reuse the namespace map of the context. |
8548 | | */ |
8549 | 0 | if (ctxt) |
8550 | 0 | nsMap = (xmlNsMapPtr) ctxt->namespaceMap; |
8551 | |
|
8552 | 0 | *resNode = NULL; |
8553 | |
|
8554 | 0 | cur = node; |
8555 | 0 | while (cur != NULL) { |
8556 | 0 | if (cur->doc != sourceDoc) { |
8557 | | /* |
8558 | | * We'll assume XIncluded nodes if the doc differs. |
8559 | | * TODO: Do we need to reconciliate XIncluded nodes? |
8560 | | * TODO: This here returns -1 in this case. |
8561 | | */ |
8562 | 0 | goto internal_error; |
8563 | 0 | } |
8564 | | /* |
8565 | | * Create a new node. |
8566 | | */ |
8567 | 0 | switch (cur->type) { |
8568 | 0 | case XML_XINCLUDE_START: |
8569 | 0 | case XML_XINCLUDE_END: |
8570 | | /* |
8571 | | * TODO: What to do with XInclude? |
8572 | | */ |
8573 | 0 | goto internal_error; |
8574 | 0 | break; |
8575 | 0 | case XML_ELEMENT_NODE: |
8576 | 0 | case XML_TEXT_NODE: |
8577 | 0 | case XML_CDATA_SECTION_NODE: |
8578 | 0 | case XML_COMMENT_NODE: |
8579 | 0 | case XML_PI_NODE: |
8580 | 0 | case XML_DOCUMENT_FRAG_NODE: |
8581 | 0 | case XML_ENTITY_REF_NODE: |
8582 | | /* |
8583 | | * Nodes of xmlNode structure. |
8584 | | */ |
8585 | 0 | clone = (xmlNodePtr) xmlMalloc(sizeof(xmlNode)); |
8586 | 0 | if (clone == NULL) |
8587 | 0 | goto internal_error; |
8588 | 0 | memset(clone, 0, sizeof(xmlNode)); |
8589 | | /* |
8590 | | * Set hierarchical links. |
8591 | | */ |
8592 | 0 | if (resultClone != NULL) { |
8593 | 0 | clone->parent = parentClone; |
8594 | 0 | if (prevClone) { |
8595 | 0 | prevClone->next = clone; |
8596 | 0 | clone->prev = prevClone; |
8597 | 0 | } else |
8598 | 0 | parentClone->children = clone; |
8599 | 0 | parentClone->last = clone; |
8600 | 0 | } else |
8601 | 0 | resultClone = clone; |
8602 | |
|
8603 | 0 | break; |
8604 | 0 | case XML_ATTRIBUTE_NODE: |
8605 | | /* |
8606 | | * Attributes (xmlAttr). |
8607 | | */ |
8608 | | /* Use xmlRealloc to avoid -Warray-bounds warning */ |
8609 | 0 | clone = (xmlNodePtr) xmlRealloc(NULL, sizeof(xmlAttr)); |
8610 | 0 | if (clone == NULL) |
8611 | 0 | goto internal_error; |
8612 | 0 | memset(clone, 0, sizeof(xmlAttr)); |
8613 | | /* |
8614 | | * Set hierarchical links. |
8615 | | * TODO: Change this to add to the end of attributes. |
8616 | | */ |
8617 | 0 | if (resultClone != NULL) { |
8618 | 0 | clone->parent = parentClone; |
8619 | 0 | if (prevClone) { |
8620 | 0 | prevClone->next = clone; |
8621 | 0 | clone->prev = prevClone; |
8622 | 0 | } else |
8623 | 0 | parentClone->properties = (xmlAttrPtr) clone; |
8624 | 0 | } else |
8625 | 0 | resultClone = clone; |
8626 | 0 | break; |
8627 | 0 | default: |
8628 | | /* |
8629 | | * TODO QUESTION: Any other nodes expected? |
8630 | | */ |
8631 | 0 | goto internal_error; |
8632 | 0 | } |
8633 | | |
8634 | 0 | clone->type = cur->type; |
8635 | 0 | clone->doc = destDoc; |
8636 | | |
8637 | | /* |
8638 | | * Clone the name of the node if any. |
8639 | | */ |
8640 | 0 | if (cur->name == xmlStringText) |
8641 | 0 | clone->name = xmlStringText; |
8642 | 0 | else if (cur->name == xmlStringTextNoenc) |
8643 | | /* |
8644 | | * NOTE: Although xmlStringTextNoenc is never assigned to a node |
8645 | | * in tree.c, it might be set in Libxslt via |
8646 | | * "xsl:disable-output-escaping". |
8647 | | */ |
8648 | 0 | clone->name = xmlStringTextNoenc; |
8649 | 0 | else if (cur->name == xmlStringComment) |
8650 | 0 | clone->name = xmlStringComment; |
8651 | 0 | else if (cur->name != NULL) { |
8652 | 0 | if (dict != NULL) |
8653 | 0 | clone->name = xmlDictLookup(dict, cur->name, -1); |
8654 | 0 | else |
8655 | 0 | clone->name = xmlStrdup(cur->name); |
8656 | 0 | if (clone->name == NULL) |
8657 | 0 | goto internal_error; |
8658 | 0 | } |
8659 | | |
8660 | 0 | switch (cur->type) { |
8661 | 0 | case XML_XINCLUDE_START: |
8662 | 0 | case XML_XINCLUDE_END: |
8663 | | /* |
8664 | | * TODO |
8665 | | */ |
8666 | 0 | return (-1); |
8667 | 0 | case XML_ELEMENT_NODE: |
8668 | 0 | cloneElem = clone; |
8669 | 0 | depth++; |
8670 | | /* |
8671 | | * Namespace declarations. |
8672 | | */ |
8673 | 0 | if (cur->nsDef != NULL) { |
8674 | 0 | if (! parnsdone) { |
8675 | 0 | if (destParent && (ctxt == NULL)) { |
8676 | | /* |
8677 | | * Gather @parent's in-scope ns-decls. |
8678 | | */ |
8679 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, |
8680 | 0 | destParent) == -1) |
8681 | 0 | goto internal_error; |
8682 | 0 | } |
8683 | 0 | parnsdone = 1; |
8684 | 0 | } |
8685 | | /* |
8686 | | * Clone namespace declarations. |
8687 | | */ |
8688 | 0 | cloneNsDefSlot = &(clone->nsDef); |
8689 | 0 | for (ns = cur->nsDef; ns != NULL; ns = ns->next) { |
8690 | | /* |
8691 | | * Create a new xmlNs. |
8692 | | */ |
8693 | 0 | cloneNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
8694 | 0 | if (cloneNs == NULL) |
8695 | 0 | goto internal_error; |
8696 | 0 | memset(cloneNs, 0, sizeof(xmlNs)); |
8697 | 0 | cloneNs->type = XML_LOCAL_NAMESPACE; |
8698 | |
|
8699 | 0 | if (ns->href != NULL) { |
8700 | 0 | cloneNs->href = xmlStrdup(ns->href); |
8701 | 0 | if (cloneNs->href == NULL) { |
8702 | 0 | xmlFreeNs(cloneNs); |
8703 | 0 | goto internal_error; |
8704 | 0 | } |
8705 | 0 | } |
8706 | 0 | if (ns->prefix != NULL) { |
8707 | 0 | cloneNs->prefix = xmlStrdup(ns->prefix); |
8708 | 0 | if (cloneNs->prefix == NULL) { |
8709 | 0 | xmlFreeNs(cloneNs); |
8710 | 0 | goto internal_error; |
8711 | 0 | } |
8712 | 0 | } |
8713 | | |
8714 | 0 | *cloneNsDefSlot = cloneNs; |
8715 | 0 | cloneNsDefSlot = &(cloneNs->next); |
8716 | | |
8717 | | /* |
8718 | | * Note that for custom handling of ns-references, |
8719 | | * the ns-decls need not be stored in the ns-map, |
8720 | | * since they won't be referenced by node->ns. |
8721 | | */ |
8722 | 0 | if ((ctxt == NULL) || |
8723 | 0 | (ctxt->getNsForNodeFunc == NULL)) |
8724 | 0 | { |
8725 | | /* |
8726 | | * Does it shadow any ns-decl? |
8727 | | */ |
8728 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8729 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8730 | 0 | if ((mi->depth >= XML_TREE_NSMAP_PARENT) && |
8731 | 0 | (mi->shadowDepth == -1) && |
8732 | 0 | ((ns->prefix == mi->newNs->prefix) || |
8733 | 0 | xmlStrEqual(ns->prefix, |
8734 | 0 | mi->newNs->prefix))) { |
8735 | | /* |
8736 | | * Mark as shadowed at the current |
8737 | | * depth. |
8738 | | */ |
8739 | 0 | mi->shadowDepth = depth; |
8740 | 0 | } |
8741 | 0 | } |
8742 | 0 | } |
8743 | | /* |
8744 | | * Push mapping. |
8745 | | */ |
8746 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8747 | 0 | ns, cloneNs, depth) == NULL) |
8748 | 0 | goto internal_error; |
8749 | 0 | } |
8750 | 0 | } |
8751 | 0 | } |
8752 | | /* cur->ns will be processed further down. */ |
8753 | 0 | break; |
8754 | 0 | case XML_ATTRIBUTE_NODE: |
8755 | | /* IDs will be processed further down. */ |
8756 | | /* cur->ns will be processed further down. */ |
8757 | 0 | break; |
8758 | 0 | case XML_PI_NODE: |
8759 | 0 | case XML_COMMENT_NODE: |
8760 | 0 | case XML_TEXT_NODE: |
8761 | 0 | case XML_CDATA_SECTION_NODE: |
8762 | | /* |
8763 | | * Note that this will also cover the values of attributes. |
8764 | | */ |
8765 | 0 | if (cur->content != NULL) { |
8766 | 0 | clone->content = xmlStrdup(cur->content); |
8767 | 0 | if (clone->content == NULL) |
8768 | 0 | goto internal_error; |
8769 | 0 | } |
8770 | 0 | goto leave_node; |
8771 | 0 | case XML_ENTITY_REF_NODE: |
8772 | 0 | if (sourceDoc != destDoc) { |
8773 | 0 | if ((destDoc->intSubset) || (destDoc->extSubset)) { |
8774 | 0 | xmlEntityPtr ent; |
8775 | | /* |
8776 | | * Different doc: Assign new entity-node if available. |
8777 | | */ |
8778 | 0 | ent = xmlGetDocEntity(destDoc, cur->name); |
8779 | 0 | if (ent != NULL) { |
8780 | 0 | clone->content = ent->content; |
8781 | 0 | clone->children = (xmlNodePtr) ent; |
8782 | 0 | clone->last = (xmlNodePtr) ent; |
8783 | 0 | } |
8784 | 0 | } |
8785 | 0 | } else { |
8786 | | /* |
8787 | | * Same doc: Use the current node's entity declaration |
8788 | | * and value. |
8789 | | */ |
8790 | 0 | clone->content = cur->content; |
8791 | 0 | clone->children = cur->children; |
8792 | 0 | clone->last = cur->last; |
8793 | 0 | } |
8794 | 0 | goto leave_node; |
8795 | 0 | default: |
8796 | 0 | goto internal_error; |
8797 | 0 | } |
8798 | | |
8799 | 0 | if (cur->ns == NULL) |
8800 | 0 | goto end_ns_reference; |
8801 | | |
8802 | | /* handle_ns_reference: */ |
8803 | | /* |
8804 | | ** The following will take care of references to ns-decls ******** |
8805 | | ** and is intended only for element- and attribute-nodes. |
8806 | | ** |
8807 | | */ |
8808 | 0 | if (! parnsdone) { |
8809 | 0 | if (destParent && (ctxt == NULL)) { |
8810 | 0 | if (xmlDOMWrapNSNormGatherInScopeNs(&nsMap, destParent) == -1) |
8811 | 0 | goto internal_error; |
8812 | 0 | } |
8813 | 0 | parnsdone = 1; |
8814 | 0 | } |
8815 | | /* |
8816 | | * Adopt ns-references. |
8817 | | */ |
8818 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8819 | | /* |
8820 | | * Search for a mapping. |
8821 | | */ |
8822 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8823 | 0 | if ((mi->shadowDepth == -1) && |
8824 | 0 | (cur->ns == mi->oldNs)) { |
8825 | | /* |
8826 | | * This is the nice case: a mapping was found. |
8827 | | */ |
8828 | 0 | clone->ns = mi->newNs; |
8829 | 0 | goto end_ns_reference; |
8830 | 0 | } |
8831 | 0 | } |
8832 | 0 | } |
8833 | | /* |
8834 | | * No matching namespace in scope. We need a new one. |
8835 | | */ |
8836 | 0 | if ((ctxt != NULL) && (ctxt->getNsForNodeFunc != NULL)) { |
8837 | | /* |
8838 | | * User-defined behaviour. |
8839 | | */ |
8840 | 0 | ns = ctxt->getNsForNodeFunc(ctxt, cur, |
8841 | 0 | cur->ns->href, cur->ns->prefix); |
8842 | | /* |
8843 | | * Add user's mapping. |
8844 | | */ |
8845 | 0 | if (xmlDOMWrapNsMapAddItem(&nsMap, -1, |
8846 | 0 | cur->ns, ns, XML_TREE_NSMAP_CUSTOM) == NULL) |
8847 | 0 | goto internal_error; |
8848 | 0 | clone->ns = ns; |
8849 | 0 | } else { |
8850 | | /* |
8851 | | * Acquire a normalized ns-decl and add it to the map. |
8852 | | */ |
8853 | 0 | if (xmlDOMWrapNSNormAcquireNormalizedNs(destDoc, |
8854 | | /* ns-decls on cloneElem or on destDoc->oldNs */ |
8855 | 0 | destParent ? cloneElem : NULL, |
8856 | 0 | cur->ns, &ns, |
8857 | 0 | &nsMap, depth, |
8858 | | /* if we need to search only in the ancestor-axis */ |
8859 | 0 | ancestorsOnly, |
8860 | | /* ns-decls must be prefixed for attributes. */ |
8861 | 0 | (cur->type == XML_ATTRIBUTE_NODE) ? 1 : 0) == -1) |
8862 | 0 | goto internal_error; |
8863 | 0 | clone->ns = ns; |
8864 | 0 | } |
8865 | | |
8866 | 0 | end_ns_reference: |
8867 | | |
8868 | | /* |
8869 | | * Some post-processing. |
8870 | | * |
8871 | | * Handle ID attributes. |
8872 | | */ |
8873 | 0 | if ((clone->type == XML_ATTRIBUTE_NODE) && |
8874 | 0 | (clone->parent != NULL)) |
8875 | 0 | { |
8876 | 0 | int res; |
8877 | |
|
8878 | 0 | res = xmlIsID(destDoc, clone->parent, (xmlAttrPtr) clone); |
8879 | 0 | if (res < 0) |
8880 | 0 | goto internal_error; |
8881 | 0 | if (res == 1) { |
8882 | 0 | xmlChar *idVal; |
8883 | |
|
8884 | 0 | idVal = xmlNodeGetContent(cur); |
8885 | 0 | if (idVal == NULL) |
8886 | 0 | goto internal_error; |
8887 | 0 | if (xmlAddIDSafe((xmlAttrPtr) cur, idVal) < 0) { |
8888 | 0 | xmlFree(idVal); |
8889 | 0 | goto internal_error; |
8890 | 0 | } |
8891 | 0 | xmlFree(idVal); |
8892 | 0 | } |
8893 | 0 | } |
8894 | | /* |
8895 | | ** |
8896 | | ** The following will traverse the tree ************************** |
8897 | | ** |
8898 | | * |
8899 | | * Walk the element's attributes before descending into child-nodes. |
8900 | | */ |
8901 | 0 | if ((cur->type == XML_ELEMENT_NODE) && (cur->properties != NULL)) { |
8902 | 0 | prevClone = NULL; |
8903 | 0 | parentClone = clone; |
8904 | 0 | cur = (xmlNodePtr) cur->properties; |
8905 | 0 | continue; |
8906 | 0 | } |
8907 | 0 | into_content: |
8908 | | /* |
8909 | | * Descend into child-nodes. |
8910 | | */ |
8911 | 0 | if (cur->children != NULL) { |
8912 | 0 | if (deep || (cur->type == XML_ATTRIBUTE_NODE)) { |
8913 | 0 | prevClone = NULL; |
8914 | 0 | parentClone = clone; |
8915 | 0 | cur = cur->children; |
8916 | 0 | continue; |
8917 | 0 | } |
8918 | 0 | } |
8919 | | |
8920 | 0 | leave_node: |
8921 | | /* |
8922 | | * At this point we are done with the node, its content |
8923 | | * and an element-nodes's attribute-nodes. |
8924 | | */ |
8925 | 0 | if (cur == node) |
8926 | 0 | break; |
8927 | 0 | if ((cur->type == XML_ELEMENT_NODE) || |
8928 | 0 | (cur->type == XML_XINCLUDE_START) || |
8929 | 0 | (cur->type == XML_XINCLUDE_END)) { |
8930 | | /* |
8931 | | * TODO: Do we expect nsDefs on XML_XINCLUDE_START? |
8932 | | */ |
8933 | 0 | if (XML_NSMAP_NOTEMPTY(nsMap)) { |
8934 | | /* |
8935 | | * Pop mappings. |
8936 | | */ |
8937 | 0 | while ((nsMap->last != NULL) && |
8938 | 0 | (nsMap->last->depth >= depth)) |
8939 | 0 | { |
8940 | 0 | XML_NSMAP_POP(nsMap, mi) |
8941 | 0 | } |
8942 | | /* |
8943 | | * Unshadow. |
8944 | | */ |
8945 | 0 | XML_NSMAP_FOREACH(nsMap, mi) { |
8946 | 0 | if (mi->shadowDepth >= depth) |
8947 | 0 | mi->shadowDepth = -1; |
8948 | 0 | } |
8949 | 0 | } |
8950 | 0 | depth--; |
8951 | 0 | } |
8952 | 0 | if (cur->next != NULL) { |
8953 | 0 | prevClone = clone; |
8954 | 0 | cur = cur->next; |
8955 | 0 | } else if (cur->type != XML_ATTRIBUTE_NODE) { |
8956 | 0 | clone = clone->parent; |
8957 | 0 | if (clone != NULL) |
8958 | 0 | parentClone = clone->parent; |
8959 | | /* |
8960 | | * Process parent --> next; |
8961 | | */ |
8962 | 0 | cur = cur->parent; |
8963 | 0 | goto leave_node; |
8964 | 0 | } else { |
8965 | | /* This is for attributes only. */ |
8966 | 0 | clone = clone->parent; |
8967 | 0 | parentClone = clone->parent; |
8968 | | /* |
8969 | | * Process parent-element --> children. |
8970 | | */ |
8971 | 0 | cur = cur->parent; |
8972 | 0 | goto into_content; |
8973 | 0 | } |
8974 | 0 | } |
8975 | 0 | goto exit; |
8976 | | |
8977 | 0 | internal_error: |
8978 | 0 | ret = -1; |
8979 | |
|
8980 | 0 | exit: |
8981 | | /* |
8982 | | * Cleanup. |
8983 | | */ |
8984 | 0 | if (nsMap != NULL) { |
8985 | 0 | if ((ctxt) && (ctxt->namespaceMap == nsMap)) { |
8986 | | /* |
8987 | | * Just cleanup the map but don't free. |
8988 | | */ |
8989 | 0 | if (nsMap->first) { |
8990 | 0 | if (nsMap->pool) |
8991 | 0 | nsMap->last->next = nsMap->pool; |
8992 | 0 | nsMap->pool = nsMap->first; |
8993 | 0 | nsMap->first = NULL; |
8994 | 0 | } |
8995 | 0 | } else |
8996 | 0 | xmlDOMWrapNsMapFree(nsMap); |
8997 | 0 | } |
8998 | | /* |
8999 | | * TODO: Should we try a cleanup of the cloned node in case of a |
9000 | | * fatal error? |
9001 | | */ |
9002 | 0 | *resNode = resultClone; |
9003 | 0 | return (ret); |
9004 | 0 | } |
9005 | | |
9006 | | /* |
9007 | | * xmlDOMWrapAdoptAttr: |
9008 | | * @ctxt: the optional context for custom processing |
9009 | | * @sourceDoc: the optional source document of attr |
9010 | | * @attr: the attribute-node to be adopted |
9011 | | * @destDoc: the destination doc for adoption |
9012 | | * @destParent: the optional new parent of @attr in @destDoc |
9013 | | * @options: option flags |
9014 | | * |
9015 | | * @attr is adopted by @destDoc. |
9016 | | * Ensures that ns-references point to @destDoc: either to |
9017 | | * elements->nsDef entries if @destParent is given, or to |
9018 | | * @destDoc->oldNs otherwise. |
9019 | | * |
9020 | | * Returns 0 if succeeded, -1 otherwise and on API/internal errors. |
9021 | | */ |
9022 | | static int |
9023 | | xmlDOMWrapAdoptAttr(xmlDOMWrapCtxtPtr ctxt, |
9024 | | xmlDocPtr sourceDoc ATTRIBUTE_UNUSED, |
9025 | | xmlAttrPtr attr, |
9026 | | xmlDocPtr destDoc, |
9027 | | xmlNodePtr destParent, |
9028 | | int options ATTRIBUTE_UNUSED) |
9029 | 0 | { |
9030 | 0 | int ret = 0; |
9031 | |
|
9032 | 0 | if ((attr == NULL) || (destDoc == NULL)) |
9033 | 0 | return (-1); |
9034 | | |
9035 | 0 | if (attr->doc != destDoc) { |
9036 | 0 | if (xmlSetTreeDoc((xmlNodePtr) attr, destDoc) < 0) |
9037 | 0 | ret = -1; |
9038 | 0 | } |
9039 | |
|
9040 | 0 | if (attr->ns != NULL) { |
9041 | 0 | xmlNsPtr ns = NULL; |
9042 | |
|
9043 | 0 | if (ctxt != NULL) { |
9044 | | /* TODO: User defined. */ |
9045 | 0 | } |
9046 | | /* XML Namespace. */ |
9047 | 0 | if (IS_STR_XML(attr->ns->prefix)) { |
9048 | 0 | ns = xmlTreeEnsureXMLDecl(destDoc); |
9049 | 0 | } else if (destParent == NULL) { |
9050 | | /* |
9051 | | * Store in @destDoc->oldNs. |
9052 | | */ |
9053 | 0 | ns = xmlDOMWrapStoreNs(destDoc, attr->ns->href, attr->ns->prefix); |
9054 | 0 | } else { |
9055 | | /* |
9056 | | * Declare on @destParent. |
9057 | | */ |
9058 | 0 | if (xmlSearchNsByNamespaceStrict(destDoc, destParent, attr->ns->href, |
9059 | 0 | &ns, 1) == -1) |
9060 | 0 | ret = -1; |
9061 | 0 | if (ns == NULL) { |
9062 | 0 | ns = xmlDOMWrapNSNormDeclareNsForced(destDoc, destParent, |
9063 | 0 | attr->ns->href, attr->ns->prefix, 1); |
9064 | 0 | } |
9065 | 0 | } |
9066 | 0 | if (ns == NULL) |
9067 | 0 | ret = -1; |
9068 | 0 | attr->ns = ns; |
9069 | 0 | } |
9070 | |
|
9071 | 0 | return (ret); |
9072 | 0 | } |
9073 | | |
9074 | | /* |
9075 | | * xmlDOMWrapAdoptNode: |
9076 | | * @ctxt: the optional context for custom processing |
9077 | | * @sourceDoc: the optional sourceDoc |
9078 | | * @node: the node to start with |
9079 | | * @destDoc: the destination doc |
9080 | | * @destParent: the optional new parent of @node in @destDoc |
9081 | | * @options: option flags |
9082 | | * |
9083 | | * References of out-of scope ns-decls are remapped to point to @destDoc: |
9084 | | * 1) If @destParent is given, then nsDef entries on element-nodes are used |
9085 | | * 2) If *no* @destParent is given, then @destDoc->oldNs entries are used |
9086 | | * This is the case when you have an unlinked node and just want to move it |
9087 | | * to the context of |
9088 | | * |
9089 | | * If @destParent is given, it ensures that the tree is namespace |
9090 | | * wellformed by creating additional ns-decls where needed. |
9091 | | * Note that, since prefixes of already existent ns-decls can be |
9092 | | * shadowed by this process, it could break QNames in attribute |
9093 | | * values or element content. |
9094 | | * NOTE: This function was not intensively tested. |
9095 | | * |
9096 | | * Returns 0 if the operation succeeded, |
9097 | | * 1 if a node of unsupported type was given, |
9098 | | * 2 if a node of not yet supported type was given and |
9099 | | * -1 on API/internal errors. |
9100 | | */ |
9101 | | int |
9102 | | xmlDOMWrapAdoptNode(xmlDOMWrapCtxtPtr ctxt, |
9103 | | xmlDocPtr sourceDoc, |
9104 | | xmlNodePtr node, |
9105 | | xmlDocPtr destDoc, |
9106 | | xmlNodePtr destParent, |
9107 | | int options) |
9108 | 0 | { |
9109 | 0 | int ret = 0; |
9110 | |
|
9111 | 0 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || |
9112 | 0 | (destDoc == NULL) || |
9113 | 0 | ((destParent != NULL) && (destParent->doc != destDoc))) |
9114 | 0 | return(-1); |
9115 | | /* |
9116 | | * Check node->doc sanity. |
9117 | | */ |
9118 | 0 | if (sourceDoc == NULL) { |
9119 | 0 | sourceDoc = node->doc; |
9120 | 0 | } else if (node->doc != sourceDoc) { |
9121 | 0 | return (-1); |
9122 | 0 | } |
9123 | | |
9124 | | /* |
9125 | | * TODO: Shouldn't this be allowed? |
9126 | | */ |
9127 | 0 | if (sourceDoc == destDoc) |
9128 | 0 | return (-1); |
9129 | | |
9130 | 0 | switch (node->type) { |
9131 | 0 | case XML_ELEMENT_NODE: |
9132 | 0 | case XML_ATTRIBUTE_NODE: |
9133 | 0 | case XML_TEXT_NODE: |
9134 | 0 | case XML_CDATA_SECTION_NODE: |
9135 | 0 | case XML_ENTITY_REF_NODE: |
9136 | 0 | case XML_PI_NODE: |
9137 | 0 | case XML_COMMENT_NODE: |
9138 | 0 | break; |
9139 | 0 | case XML_DOCUMENT_FRAG_NODE: |
9140 | | /* TODO: Support document-fragment-nodes. */ |
9141 | 0 | return (2); |
9142 | 0 | default: |
9143 | 0 | return (1); |
9144 | 0 | } |
9145 | | /* |
9146 | | * Unlink only if @node was not already added to @destParent. |
9147 | | */ |
9148 | 0 | if ((node->parent != NULL) && (destParent != node->parent)) |
9149 | 0 | xmlUnlinkNodeInternal(node); |
9150 | |
|
9151 | 0 | if (node->type == XML_ELEMENT_NODE) { |
9152 | 0 | return (xmlDOMWrapAdoptBranch(ctxt, sourceDoc, node, |
9153 | 0 | destDoc, destParent, options)); |
9154 | 0 | } else if (node->type == XML_ATTRIBUTE_NODE) { |
9155 | 0 | return (xmlDOMWrapAdoptAttr(ctxt, sourceDoc, |
9156 | 0 | (xmlAttrPtr) node, destDoc, destParent, options)); |
9157 | 0 | } else { |
9158 | 0 | if (node->doc != destDoc) { |
9159 | 0 | if (xmlNodeSetDoc(node, destDoc) < 0) |
9160 | 0 | ret = -1; |
9161 | 0 | } |
9162 | 0 | } |
9163 | 0 | return (ret); |
9164 | 0 | } |
9165 | | |
9166 | | /************************************************************************ |
9167 | | * * |
9168 | | * XHTML detection * |
9169 | | * * |
9170 | | ************************************************************************/ |
9171 | | |
9172 | 0 | #define XHTML_STRICT_PUBLIC_ID BAD_CAST \ |
9173 | 0 | "-//W3C//DTD XHTML 1.0 Strict//EN" |
9174 | 0 | #define XHTML_STRICT_SYSTEM_ID BAD_CAST \ |
9175 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" |
9176 | 0 | #define XHTML_FRAME_PUBLIC_ID BAD_CAST \ |
9177 | 0 | "-//W3C//DTD XHTML 1.0 Frameset//EN" |
9178 | 0 | #define XHTML_FRAME_SYSTEM_ID BAD_CAST \ |
9179 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd" |
9180 | 0 | #define XHTML_TRANS_PUBLIC_ID BAD_CAST \ |
9181 | 0 | "-//W3C//DTD XHTML 1.0 Transitional//EN" |
9182 | 0 | #define XHTML_TRANS_SYSTEM_ID BAD_CAST \ |
9183 | 0 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" |
9184 | | |
9185 | | /** |
9186 | | * xmlIsXHTML: |
9187 | | * @systemID: the system identifier |
9188 | | * @publicID: the public identifier |
9189 | | * |
9190 | | * Try to find if the document correspond to an XHTML DTD |
9191 | | * |
9192 | | * Returns 1 if true, 0 if not and -1 in case of error |
9193 | | */ |
9194 | | int |
9195 | 0 | xmlIsXHTML(const xmlChar *systemID, const xmlChar *publicID) { |
9196 | 0 | if ((systemID == NULL) && (publicID == NULL)) |
9197 | 0 | return(-1); |
9198 | 0 | if (publicID != NULL) { |
9199 | 0 | if (xmlStrEqual(publicID, XHTML_STRICT_PUBLIC_ID)) return(1); |
9200 | 0 | if (xmlStrEqual(publicID, XHTML_FRAME_PUBLIC_ID)) return(1); |
9201 | 0 | if (xmlStrEqual(publicID, XHTML_TRANS_PUBLIC_ID)) return(1); |
9202 | 0 | } |
9203 | 0 | if (systemID != NULL) { |
9204 | 0 | if (xmlStrEqual(systemID, XHTML_STRICT_SYSTEM_ID)) return(1); |
9205 | 0 | if (xmlStrEqual(systemID, XHTML_FRAME_SYSTEM_ID)) return(1); |
9206 | 0 | if (xmlStrEqual(systemID, XHTML_TRANS_SYSTEM_ID)) return(1); |
9207 | 0 | } |
9208 | 0 | return(0); |
9209 | 0 | } |
9210 | | |
9211 | | /************************************************************************ |
9212 | | * * |
9213 | | * Node callbacks * |
9214 | | * * |
9215 | | ************************************************************************/ |
9216 | | |
9217 | | /** |
9218 | | * xmlRegisterNodeDefault: |
9219 | | * @func: function pointer to the new RegisterNodeFunc |
9220 | | * |
9221 | | * DEPRECATED: don't use |
9222 | | * |
9223 | | * Registers a callback for node creation |
9224 | | * |
9225 | | * Returns the old value of the registration function |
9226 | | */ |
9227 | | xmlRegisterNodeFunc |
9228 | | xmlRegisterNodeDefault(xmlRegisterNodeFunc func) |
9229 | 0 | { |
9230 | 0 | xmlRegisterNodeFunc old = xmlRegisterNodeDefaultValue; |
9231 | |
|
9232 | 0 | xmlRegisterCallbacks = 1; |
9233 | 0 | xmlRegisterNodeDefaultValue = func; |
9234 | 0 | return(old); |
9235 | 0 | } |
9236 | | |
9237 | | /** |
9238 | | * xmlDeregisterNodeDefault: |
9239 | | * @func: function pointer to the new DeregisterNodeFunc |
9240 | | * |
9241 | | * DEPRECATED: don't use |
9242 | | * |
9243 | | * Registers a callback for node destruction |
9244 | | * |
9245 | | * Returns the previous value of the deregistration function |
9246 | | */ |
9247 | | xmlDeregisterNodeFunc |
9248 | | xmlDeregisterNodeDefault(xmlDeregisterNodeFunc func) |
9249 | 0 | { |
9250 | 0 | xmlDeregisterNodeFunc old = xmlDeregisterNodeDefaultValue; |
9251 | |
|
9252 | 0 | xmlRegisterCallbacks = 1; |
9253 | 0 | xmlDeregisterNodeDefaultValue = func; |
9254 | 0 | return(old); |
9255 | 0 | } |
9256 | | |