* xmlReconciliateNs: * @doc: the document * @tree: a node defining the subtree to reconciliate * * This function checks that all the namespaces declared within the given * tree are properly declared. This is needed for example after Copy or Cut * and then paste operations. The subtree may still hold pointers to * namespace declarations outside the subtree or invalid/masked. As much * as
| 6386 | * Returns 0 on success or -1 in case of error. |
| 6387 | */ |
| 6388 | int |
| 6389 | xmlReconciliateNs(xmlDocPtr doc, xmlNodePtr tree) { |
| 6390 | xmlNsCache *cache = NULL; |
| 6391 | int sizeCache = 0; |
| 6392 | int nbCache = 0; |
| 6393 | |
| 6394 | xmlNsPtr n; |
| 6395 | xmlNodePtr node = tree; |
| 6396 | xmlAttrPtr attr; |
| 6397 | int ret = 0, i; |
| 6398 | |
| 6399 | if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) return(-1); |
| 6400 | if (node->doc != doc) return(-1); |
| 6401 | while (node != NULL) { |
| 6402 | /* |
| 6403 | * Reconciliate the node namespace |
| 6404 | */ |
| 6405 | if (node->ns != NULL) { |
| 6406 | for (i = 0; i < nbCache; i++) { |
| 6407 | if (cache[i].oldNs == node->ns) { |
| 6408 | node->ns = cache[i].newNs; |
| 6409 | break; |
| 6410 | } |
| 6411 | } |
| 6412 | if (i == nbCache) { |
| 6413 | /* |
| 6414 | * OK we need to recreate a new namespace definition |
| 6415 | */ |
| 6416 | n = xmlNewReconciledNs(tree, node->ns); |
| 6417 | if (n == NULL) { |
| 6418 | ret = -1; |
| 6419 | } else { |
| 6420 | /* |
| 6421 | * check if we need to grow the cache buffers. |
| 6422 | */ |
| 6423 | if (sizeCache <= nbCache) { |
| 6424 | xmlNsCache *tmp; |
| 6425 | size_t newSize = sizeCache ? sizeCache * 2 : 10; |
| 6426 | |
| 6427 | tmp = xmlRealloc(cache, newSize * sizeof(tmp[0])); |
| 6428 | if (tmp == NULL) { |
| 6429 | ret = -1; |
| 6430 | } else { |
| 6431 | cache = tmp; |
| 6432 | sizeCache = newSize; |
| 6433 | } |
| 6434 | } |
| 6435 | if (nbCache < sizeCache) { |
| 6436 | cache[nbCache].newNs = n; |
| 6437 | cache[nbCache++].oldNs = node->ns; |
| 6438 | } |
| 6439 | } |
| 6440 | node->ns = n; |
| 6441 | } |
| 6442 | } |
| 6443 | /* |
| 6444 | * now check for namespace held by attributes on the node. |
| 6445 | */ |
no test coverage detected