📄 document.c
字号:
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2003 Intel Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither name of Intel Corporation nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "ixmlparser.h"
/*================================================================
* ixmlDocument_init
* It initialize the document structure.
* External function.
*
*=================================================================*/
void
ixmlDocument_init( IN IXML_Document * doc )
{
memset( doc, 0, sizeof( IXML_Document ) );
}
/*================================================================
* ixmlDocument_free
* It frees the whole document tree.
* External function.
*
*=================================================================*/
void
ixmlDocument_free( IN IXML_Document * doc )
{
if( doc != NULL ) {
ixmlNode_free( ( IXML_Node * ) doc );
}
}
/*================================================================
* ixmlDocument_setOwnerDocument
*
* When this function is called first time, nodeptr is the root
* of the subtree, so it is not necessay to do two steps
* recursion.
*
* Internal function called by ixmlDocument_importNode
*
*=================================================================*/
void
ixmlDocument_setOwnerDocument( IN IXML_Document * doc,
IN IXML_Node * nodeptr )
{
if( nodeptr != NULL ) {
nodeptr->ownerDocument = doc;
ixmlDocument_setOwnerDocument( doc,
ixmlNode_getFirstChild( nodeptr ) );
ixmlDocument_setOwnerDocument( doc,
ixmlNode_getNextSibling
( nodeptr ) );
}
}
/*================================================================
* ixmlDocument_importNode
* Imports a node from another document to this document. The
* returned node has no parent; (parentNode is null). The source
* node is not altered or removed from the original document;
* this method creates a new copy of the source node.
* For all nodes, importing a node creates a node object owned
* by the importing document, with attribute values identical to
* the source node's nodeName and nodeType, plus the attributes
* related to namespaces (prefix, localName, and namespaceURI).
* As in the cloneNode operation on a node, the source node is
* not altered.
*
* External function.
*
*=================================================================*/
int
ixmlDocument_importNode( IN IXML_Document * doc,
IN IXML_Node * importNode,
IN BOOL deep,
OUT IXML_Node ** rtNode )
{
unsigned short nodeType;
IXML_Node *newNode;
*rtNode = NULL;
if( ( doc == NULL ) || ( importNode == NULL ) ) {
return IXML_INVALID_PARAMETER;
}
nodeType = ixmlNode_getNodeType( importNode );
if( nodeType == eDOCUMENT_NODE ) {
return IXML_NOT_SUPPORTED_ERR;
}
newNode = ixmlNode_cloneNode( importNode, deep );
if( newNode == NULL ) {
return IXML_FAILED;
}
ixmlDocument_setOwnerDocument( doc, newNode );
*rtNode = newNode;
return IXML_SUCCESS;
}
/*================================================================
* ixmlDocument_createElementEx
* Creates an element of the type specified.
* External function.
* Parameters:
* doc: pointer to document
* tagName: The name of the element, it is case-sensitive.
* Return Value:
* IXML_SUCCESS
* IXML_INVALID_PARAMETER: if either doc or tagName is NULL
* IXML_INSUFFICIENT_MEMORY: if not enough memory to finish this operations.
*
*=================================================================*/
int
ixmlDocument_createElementEx( IN IXML_Document * doc,
IN DOMString tagName,
OUT IXML_Element ** rtElement )
{
int errCode = IXML_SUCCESS;
IXML_Element *newElement = NULL;
if( ( doc == NULL ) || ( tagName == NULL ) ) {
errCode = IXML_INVALID_PARAMETER;
goto ErrorHandler;
}
newElement = ( IXML_Element * ) malloc( sizeof( IXML_Element ) );
if( newElement == NULL ) {
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
ixmlElement_init( newElement );
newElement->tagName = strdup( tagName );
if( newElement->tagName == NULL ) {
ixmlElement_free( newElement );
newElement = NULL;
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
// set the node fields
newElement->n.nodeType = eELEMENT_NODE;
newElement->n.nodeName = strdup( tagName );
if( newElement->n.nodeName == NULL ) {
ixmlElement_free( newElement );
newElement = NULL;
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
newElement->n.ownerDocument = doc;
ErrorHandler:
*rtElement = newElement;
return errCode;
}
/*================================================================
* ixmlDocument_createElement
* Creates an element of the type specified.
* External function.
* Parameters:
* doc: pointer to document
* tagName: The name of the element, it is case-sensitive.
* Return Value:
* A new element object with the nodeName set to tagName, and
* localName, prefix and namespaceURI set to null.
*
*=================================================================*/
IXML_Element *
ixmlDocument_createElement( IN IXML_Document * doc,
IN DOMString tagName )
{
IXML_Element *newElement = NULL;
ixmlDocument_createElementEx( doc, tagName, &newElement );
return newElement;
}
/*================================================================
* ixmlDocument_createDocumentEx
* Creates an document object
* Internal function.
* Parameters:
* rtDoc: the document created or NULL on failure
* Return Value:
* IXML_SUCCESS
* IXML_INSUFFICIENT_MEMORY: if not enough memory to finish this operations.
*
*=================================================================*/
int
ixmlDocument_createDocumentEx( OUT IXML_Document ** rtDoc )
{
IXML_Document *doc;
int errCode = IXML_SUCCESS;
doc = NULL;
doc = ( IXML_Document * ) malloc( sizeof( IXML_Document ) );
if( doc == NULL ) {
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
ixmlDocument_init( doc );
doc->n.nodeName = strdup( DOCUMENTNODENAME );
if( doc->n.nodeName == NULL ) {
ixmlDocument_free( doc );
doc = NULL;
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
doc->n.nodeType = eDOCUMENT_NODE;
doc->n.ownerDocument = doc;
ErrorHandler:
*rtDoc = doc;
return errCode;
}
/*================================================================
* ixmlDocument_createDocument
* Creates an document object
* Internal function.
* Parameters:
* none
* Return Value:
* A new document object with the nodeName set to "#document".
*
*=================================================================*/
IXML_Document *
ixmlDocument_createDocument( )
{
IXML_Document *doc = NULL;
ixmlDocument_createDocumentEx( &doc );
return doc;
}
/*================================================================
* ixmlDocument_createTextNodeEx
* Creates an text node.
* External function.
* Parameters:
* data: text data for the text node. It is stored in nodeValue field.
* Return Value:
* IXML_SUCCESS
* IXML_INVALID_PARAMETER: if either doc or data is NULL
* IXML_INSUFFICIENT_MEMORY: if not enough memory to finish this operations.
*
*=================================================================*/
int
ixmlDocument_createTextNodeEx( IN IXML_Document * doc,
IN char *data,
OUT IXML_Node ** textNode )
{
IXML_Node *returnNode;
int rc = IXML_SUCCESS;
returnNode = NULL;
if( ( doc == NULL ) || ( data == NULL ) ) {
rc = IXML_INVALID_PARAMETER;
goto ErrorHandler;
}
returnNode = ( IXML_Node * ) malloc( sizeof( IXML_Node ) );
if( returnNode == NULL ) {
rc = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
// initialize the node
ixmlNode_init( returnNode );
returnNode->nodeName = strdup( TEXTNODENAME );
if( returnNode->nodeName == NULL ) {
ixmlNode_free( returnNode );
returnNode = NULL;
rc = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
// add in node value
if( data != NULL ) {
returnNode->nodeValue = strdup( data );
if( returnNode->nodeValue == NULL ) {
ixmlNode_free( returnNode );
returnNode = NULL;
rc = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
}
returnNode->nodeType = eTEXT_NODE;
returnNode->ownerDocument = doc;
ErrorHandler:
*textNode = returnNode;
return rc;
}
/*================================================================
* ixmlDocument_createTextNode
* Creates an text node.
* External function.
* Parameters:
* data: text data for the text node. It is stored in nodeValue field.
* Return Value:
* The new text node.
*
*=================================================================*/
IXML_Node *
ixmlDocument_createTextNode( IN IXML_Document * doc,
IN char *data )
{
IXML_Node *returnNode = NULL;
ixmlDocument_createTextNodeEx( doc, data, &returnNode );
return returnNode;
}
/*================================================================
* ixmlDocument_createAttributeEx
* Creates an attribute of the given name.
* External function.
* Parameters:
* name: The name of the Attribute node.
* Return Value:
* IXML_SUCCESS
* IXML_INSUFFICIENT_MEMORY: if not enough memory to finish this operations.
*
================================================================*/
int
ixmlDocument_createAttributeEx( IN IXML_Document * doc,
IN char *name,
OUT IXML_Attr ** rtAttr )
{
IXML_Attr *attrNode = NULL;
int errCode = IXML_SUCCESS;
attrNode = ( IXML_Attr * ) malloc( sizeof( IXML_Attr ) );
if( attrNode == NULL ) {
errCode = IXML_INSUFFICIENT_MEMORY;
goto ErrorHandler;
}
if( ( doc == NULL ) || ( name == NULL ) ) {
ixmlAttr_free( attrNode );
attrNode = NULL;
errCode = IXML_INVALID_PARAMETER;
goto ErrorHandler;
}
ixmlAttr_init( attrNode );
attrNode->n.nodeType = eATTRIBUTE_NODE;
// set the node fields
attrNode->n.nodeName = strdup( name );
if( attrNode->n.nodeName == NULL ) {
ixmlAttr_free( attrNode );
attrNode = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -