[xml] xmlSetTreeDoc and dictionaries



I just noticed debug messages about element names not being from dictionary.
It's due to the fact that I was creating nodes without a document and appending them to a tree. Saw that the append functions are calling xmlSetTreeDoc so never really thought about it until now.

Here is something I was playing with that only tries to fix up adding nodes created from no doc or a doc with no dict to a doc with a dict. It also only deals with fixing up node names as i havent hit a case here where content was in a dictionary so didnt want to try to fix something i wasnt reproducing. Works fine, but it seems like the xmlSetTreeDoc now starts becoming similar to the new XML_TREE_ADOPT_STR macros and started wondering if the functionality should somehow be combined.

Rob
Index: tree.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/tree.c,v
retrieving revision 1.352
diff -c -r1.352 tree.c
*** tree.c      19 Jul 2005 11:31:55 -0000      1.352
--- tree.c      25 Jul 2005 00:58:12 -0000
***************
*** 2671,2691 ****
  void
  xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
      xmlAttrPtr prop;
  
      if (tree == NULL)
!       return;
      if (tree->doc != doc) {
        if(tree->type == XML_ELEMENT_NODE) {
            prop = tree->properties;
            while (prop != NULL) {
!               prop->doc = doc;
!               xmlSetListDoc(prop->children, doc);
!               prop = prop->next;
            }
        }
        if (tree->children != NULL)
            xmlSetListDoc(tree->children, doc);
        tree->doc = doc;
      }
  }
  
--- 2671,2701 ----
  void
  xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
      xmlAttrPtr prop;
+       xmlDocPtr olddoc;
  
      if (tree == NULL)
!               return;
      if (tree->doc != doc) {
        if(tree->type == XML_ELEMENT_NODE) {
            prop = tree->properties;
            while (prop != NULL) {
!                       olddoc = prop->doc;
!                       prop->doc = doc;
!                       if (doc && doc->dict && (olddoc == NULL || olddoc->dict == NULL)) {
!                               xmlNodeSetName((xmlNodePtr) prop, prop->name);
!                       }
!                       xmlSetListDoc(prop->children, doc);
!                       prop = prop->next;
            }
        }
        if (tree->children != NULL)
            xmlSetListDoc(tree->children, doc);
+ 
+       olddoc = tree->doc;
        tree->doc = doc;
+       if (doc && doc->dict && (olddoc == NULL || olddoc->dict == NULL)) {
+               xmlNodeSetName((xmlNodePtr) tree, tree->name);
+       }
      }
  }
  
***************
*** 4619,4624 ****
--- 4629,4635 ----
  xmlNodeSetName(xmlNodePtr cur, const xmlChar *name) {
      xmlDocPtr doc;
      xmlDictPtr dict;
+       xmlChar *tmpname = NULL;
  
      if (cur == NULL) return;
      if (name == NULL) return;
***************
*** 4651,4666 ****
      }
      doc = cur->doc;
      if (doc != NULL)
!       dict = doc->dict;
      else
          dict = NULL;
      if (dict != NULL) {
!         if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
!           xmlFree((xmlChar *) cur->name);
!       cur->name = xmlDictLookup(dict, name, -1);
      } else {
!       if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
!       cur->name = xmlStrdup(name);
      }
  }
  #endif
--- 4662,4679 ----
      }
      doc = cur->doc;
      if (doc != NULL)
!               dict = doc->dict;
      else
          dict = NULL;
+       tmpname = (xmlChar *) cur->name;
+ 
      if (dict != NULL) {
!               cur->name = xmlDictLookup(dict, name, -1);
!         if ((tmpname != NULL) && (!xmlDictOwns(dict, tmpname)))
!                       xmlFree(tmpname);
      } else {
!               cur->name = xmlStrdup(name);
!               if (tmpname != NULL) xmlFree(tmpname);
      }
  }
  #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]