📄 element.cpp
字号:
//
// Element.cpp
//
// $Id: //poco/Main/XML/src/Element.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. 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.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// 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 THE
// COPYRIGHT OWNER 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 "DOM/Element.h"
#include "DOM/Document.h"
#include "DOM/Attr.h"
#include "DOM/DOMException.h"
#include "DOM/ElementsByTagNameList.h"
#include "DOM/Text.h"
#include "DOM/AttrMap.h"
XML_BEGIN
Element::Element(Document* pOwnerDocument, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname):
AbstractContainerNode(pOwnerDocument),
_name(pOwnerDocument->namePool().insert(qname, namespaceURI, localName)),
_pFirstAttr(0)
{
}
Element::Element(Document* pOwnerDocument, const Element& element):
AbstractContainerNode(pOwnerDocument, element),
_name(pOwnerDocument->namePool().insert(element._name)),
_pFirstAttr(0)
{
Attr* pAttr = element._pFirstAttr;
while (pAttr)
{
Attr* pClonedAttr = static_cast<Attr*>(pAttr->copyNode(false, pOwnerDocument));
setAttributeNode(pClonedAttr);
pClonedAttr->release();
pAttr = static_cast<Attr*>(pAttr->_pNext);
}
}
Element::~Element()
{
if (_pFirstAttr) _pFirstAttr->release();
}
const XMLString& Element::getAttribute(const XMLString& name) const
{
Attr* pAttr = getAttributeNode(name);
if (pAttr)
return pAttr->getValue();
else
return EMPTY_STRING;
}
void Element::setAttribute(const XMLString& name, const XMLString& value)
{
Attr* pAttr = getAttributeNode(name);
if (pAttr)
{
pAttr->setValue(value);
}
else
{
pAttr = ownerDocument()->createAttribute(name);
pAttr->setValue(value);
setAttributeNode(pAttr);
pAttr->release();
}
}
void Element::removeAttribute(const XMLString& name)
{
Attr* pAttr = getAttributeNode(name);
if (pAttr) removeAttributeNode(pAttr);
}
Attr* Element::getAttributeNode(const XMLString& name) const
{
Attr* pAttr = _pFirstAttr;
while (pAttr && pAttr->_name.qname() != name) pAttr = static_cast<Attr*>(pAttr->_pNext);
return pAttr;
}
Attr* Element::setAttributeNode(Attr* newAttr)
{
poco_check_ptr (newAttr);
if (newAttr->ownerDocument() != ownerDocument())
throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
if (newAttr->ownerElement())
throw DOMException(DOMException::INUSE_ATTRIBUTE_ERR);
Attr* oldAttr = getAttributeNode(newAttr->name());
if (oldAttr) removeAttributeNode(oldAttr);
Attr* pCur = _pFirstAttr;
if (pCur)
{
while (pCur->_pNext) pCur = static_cast<Attr*>(pCur->_pNext);
pCur->_pNext = newAttr;
}
else _pFirstAttr = newAttr;
newAttr->duplicate();
newAttr->_pParent = this;
if (_pOwner->events())
dispatchAttrModified(newAttr, MutationEvent::ADDITION, EMPTY_STRING, newAttr->getValue());
return oldAttr;
}
Attr* Element::removeAttributeNode(Attr* oldAttr)
{
poco_check_ptr (oldAttr);
if (_pOwner->events())
dispatchAttrModified(oldAttr, MutationEvent::REMOVAL, oldAttr->getValue(), EMPTY_STRING);
if (oldAttr != _pFirstAttr)
{
Attr* pCur = _pFirstAttr;
while (pCur->_pNext != oldAttr) pCur = static_cast<Attr*>(pCur->_pNext);
if (pCur)
{
pCur->_pNext = static_cast<Attr*>(pCur->_pNext->_pNext);
}
else throw DOMException(DOMException::NOT_FOUND_ERR);
}
else _pFirstAttr = static_cast<Attr*>(_pFirstAttr->_pNext);
oldAttr->_pNext = 0;
oldAttr->_pParent = 0;
oldAttr->autoRelease();
return oldAttr;
}
NodeList* Element::getElementsByTagName(const XMLString& name) const
{
return new ElementsByTagNameList(this, name);
}
NodeList* Element::getElementsByTagNameNS(const XMLString& namespaceURI, const XMLString& localName) const
{
return new ElementsByTagNameListNS(this, namespaceURI, localName);
}
void Element::normalize()
{
Node* pCur = firstChild();
while (pCur)
{
if (pCur->nodeType() == Node::ELEMENT_NODE)
{
pCur->normalize();
}
else if (pCur->nodeType() == Node::TEXT_NODE)
{
Node* pNext = pCur->nextSibling();
while (pNext && pNext->nodeType() == Node::TEXT_NODE)
{
static_cast<Text*>(pCur)->appendData(pNext->nodeValue());
removeChild(pNext);
pNext = pCur->nextSibling();
}
}
pCur = pCur->nextSibling();
}
}
const XMLString& Element::nodeName() const
{
return tagName();
}
NamedNodeMap* Element::attributes() const
{
return new AttrMap(const_cast<Element*>(this));
}
unsigned short Element::nodeType() const
{
return Node::ELEMENT_NODE;
}
const XMLString& Element::getAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const
{
Attr* pAttr = getAttributeNodeNS(namespaceURI, localName);
if (pAttr)
return pAttr->getValue();
else
return EMPTY_STRING;
}
void Element::setAttributeNS(const XMLString& namespaceURI, const XMLString& qualifiedName, const XMLString& value)
{
Attr* pAttr = getAttributeNodeNS(namespaceURI, qualifiedName);
if (pAttr)
{
pAttr->setValue(value);
}
else
{
pAttr = _pOwner->createAttributeNS(namespaceURI, qualifiedName);
pAttr->setValue(value);
setAttributeNodeNS(pAttr);
pAttr->release();
}
}
void Element::removeAttributeNS(const XMLString& namespaceURI, const XMLString& localName)
{
Attr* pAttr = getAttributeNodeNS(namespaceURI, localName);
if (pAttr) removeAttributeNode(pAttr);
}
Attr* Element::getAttributeNodeNS(const XMLString& namespaceURI, const XMLString& localName) const
{
Attr* pAttr = _pFirstAttr;
while (pAttr && (pAttr->_name.namespaceURI() != namespaceURI || pAttr->_name.localName() != localName)) pAttr = static_cast<Attr*>(pAttr->_pNext);
return pAttr;
}
Attr* Element::setAttributeNodeNS(Attr* newAttr)
{
poco_check_ptr (newAttr);
if (newAttr->ownerDocument() != ownerDocument())
throw DOMException(DOMException::WRONG_DOCUMENT_ERR);
if (newAttr->ownerElement())
throw DOMException(DOMException::INUSE_ATTRIBUTE_ERR);
Attr* oldAttr = getAttributeNodeNS(newAttr->namespaceURI(), newAttr->localName());
if (oldAttr) removeAttributeNode(oldAttr);
Attr* pCur = _pFirstAttr;
if (pCur)
{
while (pCur->_pNext) pCur = static_cast<Attr*>(pCur->_pNext);
pCur->_pNext = newAttr;
}
else _pFirstAttr = newAttr;
newAttr->_pParent = this;
newAttr->duplicate();
if (_pOwner->events())
dispatchAttrModified(newAttr, MutationEvent::ADDITION, EMPTY_STRING, newAttr->getValue());
return oldAttr;
}
bool Element::hasAttribute(const XMLString& name) const
{
return getAttributeNode(name) != 0;
}
bool Element::hasAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const
{
return getAttributeNodeNS(namespaceURI, localName) != 0;
}
const XMLString& Element::namespaceURI() const
{
return _name.namespaceURI();
}
XMLString Element::prefix() const
{
return _name.prefix();
}
const XMLString& Element::localName() const
{
return _name.localName();
}
bool Element::hasAttributes() const
{
return _pFirstAttr != 0;
}
Element* Element::getChildElement(const XMLString& name) const
{
Node* pNode = firstChild();
while (pNode && !(pNode->nodeType() == Node::ELEMENT_NODE && pNode->nodeName() == name))
pNode = pNode->nextSibling();
return static_cast<Element*>(pNode);
}
Element* Element::getChildElementNS(const XMLString& namespaceURI, const XMLString& localName) const
{
Node* pNode = firstChild();
while (pNode && !(pNode->nodeType() == Node::ELEMENT_NODE && pNode->namespaceURI() == namespaceURI && pNode->localName() == localName))
pNode = pNode->nextSibling();
return static_cast<Element*>(pNode);
}
void Element::dispatchNodeRemovedFromDocument()
{
AbstractContainerNode::dispatchNodeRemovedFromDocument();
Attr* pAttr = _pFirstAttr;
while (pAttr)
{
pAttr->dispatchNodeRemovedFromDocument();
pAttr = static_cast<Attr*>(pAttr->_pNext);
}
}
void Element::dispatchNodeInsertedIntoDocument()
{
AbstractContainerNode::dispatchNodeInsertedIntoDocument();
Attr* pAttr = _pFirstAttr;
while (pAttr)
{
pAttr->dispatchNodeInsertedIntoDocument();
pAttr = static_cast<Attr*>(pAttr->_pNext);
}
}
Node* Element::copyNode(bool deep, Document* pOwnerDocument) const
{
Element* pClone = new Element(pOwnerDocument, *this);
if (deep)
{
Node* pNode = firstChild();
while (pNode)
{
pClone->appendChild(static_cast<AbstractNode*>(pNode)->copyNode(true, pOwnerDocument))->release();
pNode = pNode->nextSibling();
}
}
return pClone;
}
XML_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -