[xml] function consolidation



While going through the ID stuff found that the same code needed to be added in many spots as code is duplicated all over the place.
Is there any reason it is done this way or just never cleaned up?

Here's a patch around some of the Prop functions.

Added an internal xmlNewPropInternal function which xmlNewProp, xmlNewNsProp and xmlNewNsPropEatName use. This will make sure these functions work the same as xmlNewNsProp and xmlNewNsPropEatName never checked the node type to insure it was an element.

Didn't add xmlNewDocProp but could easily be done with another paramter as well as not sure if this is a bug, but xmlNewDocProp doesnt call xmlEncodeEntitiesReentrant as the other ones do.

Also used xmlUnLinkNode within xmlUnsetProp and xmlUnsetNsProp. This will allow for any uncessary ID calls to be made properly otherwise would need to duplicate that code as well in those functions.

Rob
Index: tree.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/tree.c,v
retrieving revision 1.339
diff -c -r1.339 tree.c
*** tree.c      28 Feb 2005 10:28:20 -0000      1.339
--- tree.c      25 Mar 2005 14:04:02 -0000
***************
*** 1721,1749 ****
  }
  #endif /* LIBXML_TREE_ENABLED */
  
! #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
!     defined(LIBXML_SCHEMAS_ENABLED)
! /**
!  * xmlNewProp:
!  * @node:  the holding node
!  * @name:  the name of the attribute
!  * @value:  the value of the attribute
!  *
!  * Create a new property carried by a node.
!  * Returns a pointer to the attribute
!  */
! xmlAttrPtr
! xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
      xmlAttrPtr cur;
      xmlDocPtr doc = NULL;
  
-     if (name == NULL) {
- #ifdef DEBUG_TREE
-         xmlGenericError(xmlGenericErrorContext,
-               "xmlNewProp : name == NULL\n");
- #endif
-       return(NULL);
-     }
      if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
        return(NULL);
  
--- 1721,1731 ----
  }
  #endif /* LIBXML_TREE_ENABLED */
  
! static xmlAttrPtr xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, 
!            const xmlChar *name, const xmlChar *value, int eatname) {
      xmlAttrPtr cur;
      xmlDocPtr doc = NULL;
  
      if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
        return(NULL);
  
***************
*** 1763,1772 ****
        doc = node->doc;
        cur->doc = doc;
      }
!     if ((doc != NULL) && (doc->dict != NULL))
!         cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
!     else
!       cur->name = xmlStrdup(name);
      if (value != NULL) {
        xmlChar *buffer;
        xmlNodePtr tmp;
--- 1745,1760 ----
        doc = node->doc;
        cur->doc = doc;
      }
!       cur->ns = ns;
! 
!       if (eatname == 0) {
!               if ((doc != NULL) && (doc->dict != NULL))
!                       cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
!               else
!                       cur->name = xmlStrdup(name);
!       } else
!               cur->name = name;
! 
      if (value != NULL) {
        xmlChar *buffer;
        xmlNodePtr tmp;
***************
*** 1777,1783 ****
        tmp = cur->children;
        while (tmp != NULL) {
            tmp->parent = (xmlNodePtr) cur;
-           tmp->doc = doc;
            if (tmp->next == NULL)
                cur->last = tmp;
            tmp = tmp->next;
--- 1765,1770 ----
***************
*** 1804,1809 ****
--- 1791,1821 ----
        xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
      return(cur);
  }
+ 
+ #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
+     defined(LIBXML_SCHEMAS_ENABLED)
+ /**
+  * xmlNewProp:
+  * @node:  the holding node
+  * @name:  the name of the attribute
+  * @value:  the value of the attribute
+  *
+  * Create a new property carried by a node.
+  * Returns a pointer to the attribute
+  */
+ xmlAttrPtr
+ xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
+ 
+     if (name == NULL) {
+ #ifdef DEBUG_TREE
+         xmlGenericError(xmlGenericErrorContext,
+               "xmlNewProp : name == NULL\n");
+ #endif
+       return(NULL);
+     }
+ 
+       return xmlNewPropInternal(node, NULL, name, value, 0);
+ }
  #endif /* LIBXML_TREE_ENABLED */
  
  /**
***************
*** 1819,1826 ****
  xmlAttrPtr
  xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
             const xmlChar *value) {
-     xmlAttrPtr cur;
-     xmlDocPtr doc = NULL;
  
      if (name == NULL) {
  #ifdef DEBUG_TREE
--- 1831,1836 ----
***************
*** 1830,1891 ****
        return(NULL);
      }
  
!     /*
!      * Allocate a new property and fill the fields.
!      */
!     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
!     if (cur == NULL) {
!       xmlTreeErrMemory("building attribute");
!       return(NULL);
!     }
!     memset(cur, 0, sizeof(xmlAttr));
!     cur->type = XML_ATTRIBUTE_NODE;
! 
!     cur->parent = node; 
!     if (node != NULL) {
!       doc = node->doc;
!       cur->doc = doc;
!     }
!     cur->ns = ns;
!     if ((doc != NULL) && (doc->dict != NULL))
!       cur->name = xmlDictLookup(doc->dict, name, -1);
!     else
!       cur->name = xmlStrdup(name);
!     if (value != NULL) {
!       xmlChar *buffer;
!       xmlNodePtr tmp;
! 
!       buffer = xmlEncodeEntitiesReentrant(doc, value);
!       cur->children = xmlStringGetNodeList(doc, buffer);
!       cur->last = NULL;
!       tmp = cur->children;
!       while (tmp != NULL) {
!           tmp->parent = (xmlNodePtr) cur;
!           if (tmp->next == NULL)
!               cur->last = tmp;
!           tmp = tmp->next;
!       }
!       xmlFree(buffer);
!     }
! 
!     /*
!      * Add it at the end to preserve parsing order ...
!      */
!     if (node != NULL) {
!       if (node->properties == NULL) {
!           node->properties = cur;
!       } else {
!           xmlAttrPtr prev = node->properties;
! 
!           while (prev->next != NULL) prev = prev->next;
!           prev->next = cur;
!           cur->prev = prev;
!       }
!     }
! 
!     if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
!       xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
!     return(cur);
  }
  
  /**
--- 1840,1846 ----
        return(NULL);
      }
  
!     return xmlNewPropInternal(node, ns, name, value, 0);
  }
  
  /**
***************
*** 1901,1908 ****
  xmlAttrPtr
  xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
             const xmlChar *value) {
-     xmlAttrPtr cur;
-     xmlDocPtr doc = NULL;
  
      if (name == NULL) {
  #ifdef DEBUG_TREE
--- 1856,1861 ----
***************
*** 1912,1970 ****
        return(NULL);
      }
  
!     /*
!      * Allocate a new property and fill the fields.
!      */
!     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
!     if (cur == NULL) {
!       xmlTreeErrMemory("building attribute");
!       return(NULL);
!     }
!     memset(cur, 0, sizeof(xmlAttr));
!     cur->type = XML_ATTRIBUTE_NODE;
! 
!     cur->parent = node; 
!     if (node != NULL) {
!       doc = node->doc;
!       cur->doc = doc;
!     }
!     cur->ns = ns;
!     cur->name = name;
!     if (value != NULL) {
!       xmlChar *buffer;
!       xmlNodePtr tmp;
! 
!       buffer = xmlEncodeEntitiesReentrant(doc, value);
!       cur->children = xmlStringGetNodeList(doc, buffer);
!       cur->last = NULL;
!       tmp = cur->children;
!       while (tmp != NULL) {
!           tmp->parent = (xmlNodePtr) cur;
!           if (tmp->next == NULL)
!               cur->last = tmp;
!           tmp = tmp->next;
!       }
!       xmlFree(buffer);
!     }
! 
!     /*
!      * Add it at the end to preserve parsing order ...
!      */
!     if (node != NULL) {
!       if (node->properties == NULL) {
!           node->properties = cur;
!       } else {
!           xmlAttrPtr prev = node->properties;
! 
!           while (prev->next != NULL) prev = prev->next;
!           prev->next = cur;
!           cur->prev = prev;
!       }
!     }
! 
!     if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
!       xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
!     return(cur);
  }
  
  /**
--- 1865,1871 ----
        return(NULL);
      }
  
!       return xmlNewPropInternal(node, ns, name, value, 1);
  }
  
  /**
***************
*** 6311,6327 ****
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
            (prop->ns == NULL)) {
!           if (prev == NULL) {
!               node->properties = prop->next;
!               if (prop->next != NULL)
!                   prop->next->prev = NULL;
!           } else {
!               prev->next = prop->next;
!               if (prop->next != NULL)
!                   prop->next->prev = NULL;
!           }
!           prop->next = NULL;
!           prop->prev = NULL;
            xmlFreeProp(prop);
            return(0);
        }
--- 6212,6218 ----
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
            (prop->ns == NULL)) {
!               xmlUnlinkNode((xmlNodePtr) prop);
            xmlFreeProp(prop);
            return(0);
        }
***************
*** 6354,6370 ****
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
            (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
!           if (prev == NULL) {
!               node->properties = prop->next;
!               if (prop->next != NULL)
!                   prop->next->prev = NULL;
!           } else {
!               prev->next = prop->next;
!               if (prop->next != NULL)
!                   prop->next->prev = NULL;
!           }
!           prop->next = NULL;
!           prop->prev = NULL;
            xmlFreeProp(prop);
            return(0);
        }
--- 6245,6251 ----
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
            (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
!           xmlUnlinkNode((xmlNodePtr) prop);
            xmlFreeProp(prop);
            return(0);
        }


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