domnodeimpl.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,113 行 · 第 1/3 页

CPP
1,113
字号
    if (thisAncestorType == DOMNode::ATTRIBUTE_NODE &&            otherAncestorType == DOMNode::ATTRIBUTE_NODE &&            thisNode==otherNode)        return DOMNode::TREE_POSITION_EQUIVALENT;    // Now, find the ancestor of the owning element, if the original    // ancestor was an attribute    if (thisAncestorType == DOMNode::ATTRIBUTE_NODE) {        thisDepth=0;        for (node=thisNode; node != 0; node = node->getParentNode()) {            thisDepth +=1;            if (node == otherNode)                // The other node is an ancestor of the owning element                return DOMNode::TREE_POSITION_PRECEDING;            thisAncestor = node;        }        for (node=otherNode; node != 0; node = node->getParentNode()) {            if (node == thisNode)                // The other node is an ancestor of the owning element                return DOMNode::TREE_POSITION_FOLLOWING;        }    }    // Now, find the ancestor of the owning element, if the original    // ancestor was an attribute    if (otherAncestorType == DOMNode::ATTRIBUTE_NODE) {        otherDepth=0;        for (node=otherNode; node != 0; node = node->getParentNode()) {            otherDepth +=1;            if (node == thisNode)                // The other node is a descendent of the reference                // node's element                return DOMNode::TREE_POSITION_FOLLOWING;            otherAncestor = node;        }        for (node=thisNode; node != 0; node = node->getParentNode()) {            if (node == otherNode)                // The other node is an ancestor of the owning element                return DOMNode::TREE_POSITION_PRECEDING;        }    }    // thisAncestor and otherAncestor must be the same at this point,    // otherwise, we are not in the same tree or document fragment    if (thisAncestor != otherAncestor)        return DOMNode::TREE_POSITION_DISCONNECTED;    // Determine which node is of the greatest depth.    if (thisDepth > otherDepth) {        for (int i= 0 ; i < thisDepth - otherDepth; i++)            thisNode = thisNode->getParentNode();    }    else {        for (int i = 0; i < otherDepth - thisDepth; i++)            otherNode = otherNode->getParentNode();    }    // We now have nodes at the same depth in the tree.  Find a common    // ancestor.    DOMNode *thisNodeP, *otherNodeP;    for (thisNodeP = thisNode->getParentNode(),                 otherNodeP = otherNode->getParentNode();             thisNodeP != otherNodeP;) {        thisNode = thisNodeP;        otherNode = otherNodeP;        thisNodeP = thisNodeP->getParentNode();        otherNodeP = otherNodeP->getParentNode();    }    // See whether thisNode or otherNode is the leftmost    for (DOMNode *current = thisNodeP->getFirstChild();             current != 0;             current = current->getNextSibling()) {        if (current == otherNode) {            return DOMNode::TREE_POSITION_PRECEDING;        }        else if (current == thisNode) {            return DOMNode::TREE_POSITION_FOLLOWING;        }    }    // REVISIT:  shouldn't get here.   Should probably throw an    // exception    return 0;}short DOMNodeImpl::reverseTreeOrderBitPattern(short pattern) const {    if(pattern & DOMNode::TREE_POSITION_PRECEDING) {        pattern &= !DOMNode::TREE_POSITION_PRECEDING;        pattern |= DOMNode::TREE_POSITION_FOLLOWING;    }    else if(pattern & DOMNode::TREE_POSITION_FOLLOWING) {        pattern &= !DOMNode::TREE_POSITION_FOLLOWING;        pattern |= DOMNode::TREE_POSITION_PRECEDING;    }    if(pattern & DOMNode::TREE_POSITION_ANCESTOR) {        pattern &= !DOMNode::TREE_POSITION_ANCESTOR;        pattern |= DOMNode::TREE_POSITION_DESCENDANT;    }    else if(pattern & DOMNode::TREE_POSITION_DESCENDANT) {        pattern &= !DOMNode::TREE_POSITION_DESCENDANT;        pattern |= DOMNode::TREE_POSITION_ANCESTOR;    }    return pattern;}/*** * *   Excerpt from http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/core.html#Node3-textContent * *   textContent of type DOMString, introduced in DOM Level 3  *    *   This attribute returns the text content of this node and its descendants. When it is defined  *   to be null, setting it has no effect.  *    *   When set, any possible children this node may have are removed and replaced by a single Text node  *   containing the string this attribute is set to.  * *   On getting, no serialization is performed, the returned string does not contain any markup.  *   No whitespace normalization is performed, the returned string does not contain the element content  *   whitespaces Fundamental Interfaces.  * *   Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content. * *   The string returned is made of the text content of this node depending on its type,  *   as defined below:  * *       Node type                                           Content           *   ====================       ======================================================================== *     ELEMENT_NODE               concatenation of the textContent attribute value of every child node,  *     ENTITY_NODE			      excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes.  *     ENTITY_REFERENCE_NODE	  This is the empty string if the node has no children.  *     DOCUMENT_FRAGMENT_NODE  *    -------------------------------------------------------------------------------------------------- *     ATTRIBUTE_NODE *     TEXT_NODE *     CDATA_SECTION_NODE *     COMMENT_NODE,  *     PROCESSING_INSTRUCTION_NODE   nodeValue  *    -------------------------------------------------------------------------------------------------- *     DOCUMENT_NODE,  *     DOCUMENT_TYPE_NODE,  *     NOTATION_NODE                 null  * ***/const XMLCh*     DOMNodeImpl::getTextContent() const{	unsigned int nBufferLength = 0;	getTextContent(NULL, nBufferLength);	XMLCh* pzBuffer = (XMLCh*)((DOMDocumentImpl*)getOwnerDocument())->allocate((nBufferLength+1) * sizeof(XMLCh));	getTextContent(pzBuffer, nBufferLength);	pzBuffer[nBufferLength] = 0;	return pzBuffer;}const XMLCh*    DOMNodeImpl::getTextContent(XMLCh* pzBuffer, unsigned int& rnBufferLength) const{	unsigned int nRemainingBuffer = rnBufferLength;	rnBufferLength = 0;	if (pzBuffer)   		*pzBuffer = 0;	DOMNode *thisNode = castToNode(this);	switch (thisNode->getNodeType())	{	case DOMNode::ELEMENT_NODE:    case DOMNode::ENTITY_NODE:    case DOMNode::ENTITY_REFERENCE_NODE:    case DOMNode::DOCUMENT_FRAGMENT_NODE:    {		DOMNode* current = thisNode->getFirstChild();		while (current != NULL) 		{			if (current->getNodeType() != DOMNode::COMMENT_NODE &&				current->getNodeType() != DOMNode::PROCESSING_INSTRUCTION_NODE)			{				if (pzBuffer)				{					unsigned int nContentLength = nRemainingBuffer;					castToNodeImpl(current)->getTextContent(pzBuffer + rnBufferLength, nContentLength);					rnBufferLength += nContentLength;					nRemainingBuffer -= nContentLength;				}				else 				{					unsigned int nContentLength = 0;					castToNodeImpl(current)->getTextContent(NULL, nContentLength);					rnBufferLength += nContentLength;				}			}			current = current->getNextSibling();		}    }    break;    case DOMNode::ATTRIBUTE_NODE:    case DOMNode::TEXT_NODE:    case DOMNode::CDATA_SECTION_NODE:    case DOMNode::COMMENT_NODE:    case DOMNode::PROCESSING_INSTRUCTION_NODE:    {		const XMLCh* pzValue = thisNode->getNodeValue();		unsigned int nStrLen = XMLString::stringLen(pzValue);		if (pzBuffer) 		{			unsigned int nContentLength = (nRemainingBuffer >= nStrLen) ? nStrLen : nRemainingBuffer;			XMLString::copyNString(pzBuffer + rnBufferLength, pzValue, nContentLength);			rnBufferLength += nContentLength;			nRemainingBuffer -= nContentLength;		}		else 		{			rnBufferLength += nStrLen;		}    }    break;	/***         DOCUMENT_NODE		 DOCUMENT_TYPE_NODE		 NOTATION_NODE	***/	default:		break;	}	return pzBuffer;}void DOMNodeImpl::setTextContent(const XMLCh* textContent){    DOMNode *thisNode = castToNode(this);    switch (thisNode->getNodeType())     {        case DOMNode::ELEMENT_NODE:        case DOMNode::ENTITY_NODE:        case DOMNode::ENTITY_REFERENCE_NODE:        case DOMNode::DOCUMENT_FRAGMENT_NODE:            {                if (isReadOnly())                  throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);                // Remove all childs                DOMNode* current = thisNode->getFirstChild();                while (current != NULL)                 {                    thisNode->removeChild(current);                    current = thisNode->getFirstChild();                }                if (textContent != NULL)                 {                    // Add textnode containing data                    current = ((DOMDocumentImpl*)thisNode->getOwnerDocument())->createTextNode(textContent);                    thisNode->appendChild(current);                }            }            break;        case DOMNode::ATTRIBUTE_NODE:        case DOMNode::TEXT_NODE:        case DOMNode::CDATA_SECTION_NODE:        case DOMNode::COMMENT_NODE:        case DOMNode::PROCESSING_INSTRUCTION_NODE:            if (isReadOnly())                throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);            thisNode->setNodeValue(textContent);            break;        case DOMNode::DOCUMENT_NODE:        case DOMNode::DOCUMENT_TYPE_NODE:        case DOMNode::NOTATION_NODE:            break;        default:            throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0, GetDOMNodeMemoryManager);    }}bool DOMNodeImpl::isDefaultNamespace(const XMLCh* namespaceURI) const{	DOMNode *thisNode = castToNode(this);    short type = thisNode->getNodeType();    switch (type) {    case DOMNode::ELEMENT_NODE: {        const XMLCh *prefix = thisNode->getPrefix();        // REVISIT: is it possible that prefix is empty string?        if (prefix == 0 || !*prefix) {            return XMLString::equals(namespaceURI, thisNode->getNamespaceURI());        }        if (thisNode->hasAttributes()) {            DOMElement *elem = (DOMElement *)thisNode;            DOMNode *attr = elem->getAttributeNodeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString);            if (attr != 0) {                const XMLCh *value = attr->getNodeValue();                return XMLString::equals(namespaceURI, value);            }        }        DOMNode *ancestor = getElementAncestor(thisNode);        if (ancestor != 0) {            return ancestor->isDefaultNamespace(namespaceURI);        }        return false;    }    case DOMNode::DOCUMENT_NODE:{        return ((DOMDocument*)thisNode)->getDocumentElement()->isDefaultNamespace(namespaceURI);    }    case DOMNode::ENTITY_NODE :    case DOMNode::NOTATION_NODE:    case DOMNode::DOCUMENT_FRAGMENT_NODE:    case DOMNode::DOCUMENT_TYPE_NODE:        // type is unknown        return false;    case DOMNode::ATTRIBUTE_NODE:{        if (fOwnerNode->getNodeType() == DOMNode::ELEMENT_NODE) {            return fOwnerNode->isDefaultNamespace(namespaceURI);        }        return false;    }    default:{        DOMNode *ancestor = getElementAncestor(thisNode);        if (ancestor != 0) {            return ancestor->isDefaultNamespace(namespaceURI);        }        return false;    }    }}DOMNode*         DOMNodeImpl::getInterface(const XMLCh*)      {    throw DOMException(DOMException::NOT_SUPPORTED_ERR, 0, GetDOMNodeMemoryManager);    return 0;}// non-standard extensionvoid DOMNodeImpl::release(){    // shouldn't reach here    throw DOMException(DOMException::INVALID_ACCESS_ERR,0, GetDOMNodeMemoryManager);}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?