📄 node.c
字号:
* External function.
*
*=================================================================*/
IXML_Node *
ixmlNode_getLastChild( IN IXML_Node * nodeptr )
{
IXML_Node *prev,
*next;
if( nodeptr != NULL ) {
prev = nodeptr;
next = nodeptr->firstChild;
while( next != NULL ) {
prev = next;
next = next->nextSibling;
}
return prev;
} else {
return NULL;
}
}
/*================================================================
* ixmlNode_getPreviousSibling
* returns the previous sibling node of nodeptr.
* External function.
*
*=================================================================*/
IXML_Node *
ixmlNode_getPreviousSibling( IN IXML_Node * nodeptr )
{
if( nodeptr != NULL ) {
return nodeptr->prevSibling;
} else {
return NULL;
}
}
/*================================================================
* ixmlNode_getNextSibling
* Returns the next sibling node.
* External function.
*
*=================================================================*/
IXML_Node *
ixmlNode_getNextSibling( IN IXML_Node * nodeptr )
{
if( nodeptr != NULL ) {
return nodeptr->nextSibling;
} else {
return NULL;
}
}
/*================================================================
* ixmlNode_getOwnerDocument
* Returns the owner document node.
* External function.
*
*=================================================================*/
IXML_Document *
ixmlNode_getOwnerDocument( IN IXML_Node * nodeptr )
{
if( nodeptr != NULL ) {
return ( IXML_Document * ) nodeptr->ownerDocument;
} else {
return NULL;
}
}
/*================================================================
* ixmlNode_isAncestor
* check if ancestorNode is ancestor of toFind
* Internal to parser only.
* Returns:
* TRUE or FALSE
*
*=================================================================*/
BOOL
ixmlNode_isAncestor( IXML_Node * ancestorNode,
IXML_Node * toFind )
{
BOOL found = FALSE;
if( ( ancestorNode != NULL ) && ( toFind != NULL ) ) {
if( toFind->parentNode == ancestorNode ) {
return TRUE;
} else {
found =
ixmlNode_isAncestor( ancestorNode->firstChild, toFind );
if( found == FALSE ) {
found =
ixmlNode_isAncestor( ancestorNode->nextSibling,
toFind );
}
}
}
return found;
}
/*================================================================
* ixmlNode_isParent
* Check whether toFind is a children of nodeptr.
* Internal to parser only.
* Return:
* TRUE or FALSE
*
*=================================================================*/
BOOL
ixmlNode_isParent( IXML_Node * nodeptr,
IXML_Node * toFind )
{
BOOL found = FALSE;
assert( nodeptr != NULL && toFind != NULL );
if( toFind->parentNode == nodeptr ) {
found = TRUE;
}
return found;
}
/*================================================================
* ixmlNode_allowChildren
* Check to see whether nodeptr allows children of type newChild.
* Internal to parser only.
* Returns:
* TRUE, if nodeptr can have newChild as children
* FALSE,if nodeptr cannot have newChild as children
*
*=================================================================*/
BOOL
ixmlNode_allowChildren( IXML_Node * nodeptr,
IXML_Node * newChild )
{
assert( nodeptr != NULL && newChild != NULL );
switch ( nodeptr->nodeType ) {
case eATTRIBUTE_NODE:
case eTEXT_NODE:
case eCDATA_SECTION_NODE:
return FALSE;
break;
case eELEMENT_NODE:
if( ( newChild->nodeType == eATTRIBUTE_NODE ) ||
( newChild->nodeType == eDOCUMENT_NODE ) ) {
return FALSE;
}
break;
case eDOCUMENT_NODE:
if( newChild->nodeType != eELEMENT_NODE ) {
return FALSE;
}
default:
break;
}
return TRUE;
}
/*================================================================
* ixmlNode_compare
* Compare two nodes to see whether they are the same node.
* Parent, sibling and children node are ignored.
* Internal to parser only.
* Returns:
* TRUE, the two nodes are the same.
* FALSE, the two nodes are not the same.
*
*=================================================================*/
BOOL
ixmlNode_compare( IXML_Node * srcNode,
IXML_Node * destNode )
{
assert( srcNode != NULL && destNode != NULL );
if( ( srcNode == destNode ) ||
( strcmp( srcNode->nodeName, destNode->nodeName ) == 0 &&
strcmp( srcNode->nodeValue, destNode->nodeValue ) == 0 &&
( srcNode->nodeType == destNode->nodeType ) &&
strcmp( srcNode->namespaceURI, destNode->namespaceURI ) == 0 &&
strcmp( srcNode->prefix, destNode->prefix ) == 0 &&
strcmp( srcNode->localName, destNode->localName ) == 0 ) ) {
return TRUE;
} else {
return FALSE;
}
}
/*================================================================
* ixmlNode_insertBefore
* Inserts the node newChild before the existing child node refChild.
* If refChild is null, insert newChild at the end of the list of
* children. If the newChild is already in the tree, it is first
* removed.
* External function.
* Parameters:
* newChild: the node to insert.
* Returns:
*
*=================================================================*/
int
ixmlNode_insertBefore( IN IXML_Node * nodeptr,
IN IXML_Node * newChild,
IN IXML_Node * refChild )
{
int ret = IXML_SUCCESS;
if( ( nodeptr == NULL ) || ( newChild == NULL ) ) {
return IXML_INVALID_PARAMETER;
}
// whether nodeptr allow children of the type of newChild
if( ixmlNode_allowChildren( nodeptr, newChild ) == FALSE ) {
return IXML_HIERARCHY_REQUEST_ERR;
}
// or if newChild is one of nodeptr's ancestors
if( ixmlNode_isAncestor( newChild, nodeptr ) == TRUE ) {
return IXML_HIERARCHY_REQUEST_ERR;
}
// if newChild was created from a different document
if( nodeptr->ownerDocument != newChild->ownerDocument ) {
return IXML_WRONG_DOCUMENT_ERR;
}
// if refChild is not a child of nodeptr
if( ixmlNode_isParent( nodeptr, refChild ) == FALSE ) {
return IXML_NOT_FOUND_ERR;
}
if( refChild != NULL ) {
if( ixmlNode_isParent( nodeptr, newChild ) == TRUE ) {
ixmlNode_removeChild( nodeptr, newChild, NULL );
newChild->nextSibling = NULL;
newChild->prevSibling = NULL;
}
newChild->nextSibling = refChild;
if( refChild->prevSibling != NULL ) {
( refChild->prevSibling )->nextSibling = newChild;
newChild->prevSibling = refChild->prevSibling;
}
refChild->prevSibling = newChild;
if( newChild->prevSibling == NULL ) {
nodeptr->firstChild = newChild;
}
newChild->parentNode = nodeptr;
} else {
ret = ixmlNode_appendChild( nodeptr, newChild );
}
return ret;
}
/*================================================================
* ixmlNode_replaceChild
* Replaces the child node oldChild with newChild in the list of children,
* and returns the oldChild node.
* External function.
* Parameters:
* newChild: the new node to put in the child list.
* oldChild: the node being replaced in the list.
* returnNode: the node replaced.
* Return Value:
* IXML_SUCCESS
* IXML_INVALID_PARAMETER: if anyone of nodeptr, newChild or oldChild is NULL.
* IXML_HIERARCHY_REQUEST_ERR: if the newChild is ancestor of nodeptr or nodeptr
* is of a type that does not allow children of the
* type of the newChild node.
* IXML_WRONG_DOCUMENT_ERR: if newChild was created from a different document than
* the one that created this node.
* IXML_NOT_FOUND_ERR: if oldChild is not a child of nodeptr.
*
*=================================================================*/
int
ixmlNode_replaceChild( IN IXML_Node * nodeptr,
IN IXML_Node * newChild,
IN IXML_Node * oldChild,
OUT IXML_Node ** returnNode )
{
int ret = IXML_SUCCESS;
if( ( nodeptr == NULL ) || ( newChild == NULL )
|| ( oldChild == NULL ) ) {
return IXML_INVALID_PARAMETER;
}
// if nodetype of nodeptr does not allow children of the type of newChild
// needs to add later
// or if newChild is one of nodeptr's ancestors
if( ixmlNode_isAncestor( newChild, nodeptr ) == TRUE ) {
return IXML_HIERARCHY_REQUEST_ERR;
}
if( ixmlNode_allowChildren( nodeptr, newChild ) == FALSE ) {
return IXML_HIERARCHY_REQUEST_ERR;
}
// if newChild was created from a different document
if( nodeptr->ownerDocument != newChild->ownerDocument ) {
return IXML_WRONG_DOCUMENT_ERR;
}
// if refChild is not a child of nodeptr
if( ixmlNode_isParent( nodeptr, oldChild ) != TRUE ) {
return IXML_NOT_FOUND_ERR;
}
ret = ixmlNode_insertBefore( nodeptr, newChild, oldChild );
if( ret != IXML_SUCCESS ) {
return ret;
}
ret = ixmlNode_removeChild( nodeptr, oldChild, returnNode );
return ret;
}
/*================================================================
* ixmlNode_removeChild
* Removes the child node indicated by oldChild from the list of
* children, and returns it.
* External function.
* Parameters:
* oldChild: the node being removed.
* returnNode: the node removed.
* Return Value:
* IXML_SUCCESS
* IXML_NOT_FOUND_ERR: if oldChild is not a child of this node.
* IXML_INVALID_PARAMETER: if either oldChild or nodeptr is NULL
*
*=================================================================*/
int
ixmlNode_removeChild( IN IXML_Node * nodeptr,
IN IXML_Node * oldChild,
OUT IXML_Node ** returnNode )
{
if( ( nodeptr == NULL ) || ( oldChild == NULL ) ) {
return IXML_INVALID_PARAMETER;
}
if( ixmlNode_isParent( nodeptr, oldChild ) == FALSE ) {
return IXML_NOT_FOUND_ERR;
}
if( oldChild->prevSibling != NULL ) {
( oldChild->prevSibling )->nextSibling = oldChild->nextSibling;
}
if( nodeptr->firstChild == oldChild ) {
nodeptr->firstChild = oldChild->nextSibling;
}
if( oldChild->nextSibling != NULL ) {
( oldChild->nextSibling )->prevSibling = oldChild->prevSibling;
}
oldChild->nextSibling = NULL;
oldChild->prevSibling = NULL;
oldChild->parentNode = NULL;
if( returnNode != NULL ) {
*returnNode = oldChild;
}
return IXML_SUCCESS;
}
/*=============================================================================
* ixmlNode_appendChild
* Adds the node newChild to the end of the list of children of this node.
* If the newChild is already in the tree, it is first removed.
* External function.
* Parameter:
* newChild: the node to add.
* Return Value:
* IXML_SUCCESS
* IXML_INVALID_PARAMETER: if either nodeptr or newChild is NULL
* IXML_WRONG_DOCUMENT_ERR: if newChild was created from a different document than
* the one that created nodeptr.
* IXML_HIERARCHY_REQUEST_ERR: if newChild is ancestor of nodeptr or if nodeptr is of
* a type that does not allow children of the type of the
* newChild node.
*
*=================================================================*/
int
ixmlNode_appendChild( IN IXML_Node * nodeptr,
IN IXML_Node * newChild )
{
IXML_Node *prev = NULL,
*next = NULL;
if( ( nodeptr == NULL ) || ( newChild == NULL ) ) {
return IXML_INVALID_PARAMETER;
}
// if newChild was created from a different document
if( ( newChild->ownerDocument != NULL ) &&
( nodeptr->ownerDocument != newChild->ownerDocument ) ) {
return IXML_WRONG_DOCUMENT_ERR;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -