📄 abstractnode.cpp
字号:
//
// AbstractNode.cpp
//
// $Id: //poco/Main/XML/src/AbstractNode.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/AbstractNode.h"
#include "DOM/Document.h"
#include "DOM/ChildNodesList.h"
#include "DOM/EventDispatcher.h"
#include "DOM/DOMException.h"
#include "DOM/EventException.h"
#include "DOM/DOMImplementation.h"
#include "DOM/Attr.h"
#include "XML/Name.h"
#include "DOM/AutoPtr.h"
XML_BEGIN
const XMLString AbstractNode::NODE_NAME = toXMLString("#node");
const XMLString AbstractNode::EMPTY_STRING;
AbstractNode::AbstractNode(Document* pOwnerDocument):
_pParent(0),
_pNext(0),
_pOwner(pOwnerDocument),
_pEventDispatcher(0)
{
}
AbstractNode::AbstractNode(Document* pOwnerDocument, const AbstractNode& node):
_pParent(0),
_pNext(0),
_pOwner(pOwnerDocument),
_pEventDispatcher(0)
{
}
AbstractNode::~AbstractNode()
{
delete _pEventDispatcher;
if (_pNext) _pNext->release();
}
void AbstractNode::autoRelease()
{
_pOwner->autoReleasePool().add(this);
}
const XMLString& AbstractNode::nodeName() const
{
return NODE_NAME;
}
const XMLString& AbstractNode::getNodeValue() const
{
return EMPTY_STRING;
}
void AbstractNode::setNodeValue(const XMLString& value)
{
throw DOMException(DOMException::NO_DATA_ALLOWED_ERR);
}
Node* AbstractNode::parentNode() const
{
return _pParent;
}
NodeList* AbstractNode::childNodes() const
{
return new ChildNodesList(this);
}
Node* AbstractNode::firstChild() const
{
return 0;
}
Node* AbstractNode::lastChild() const
{
return 0;
}
Node* AbstractNode::previousSibling() const
{
if (_pParent)
{
AbstractNode* pSibling = _pParent->_pFirstChild;
while (pSibling)
{
if (pSibling->_pNext == this) return pSibling;
pSibling = pSibling->_pNext;
}
}
return 0;
}
Node* AbstractNode::nextSibling() const
{
return _pNext;
}
NamedNodeMap* AbstractNode::attributes() const
{
return 0;
}
Document* AbstractNode::ownerDocument() const
{
return _pOwner;
}
Node* AbstractNode::insertBefore(Node* newChild, Node* refChild)
{
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
}
Node* AbstractNode::replaceChild(Node* newChild, Node* oldChild)
{
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
}
Node* AbstractNode::removeChild(Node* oldChild)
{
throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR);
}
Node* AbstractNode::appendChild(Node* newChild)
{
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
}
bool AbstractNode::hasChildNodes() const
{
return false;
}
Node* AbstractNode::cloneNode(bool deep) const
{
return copyNode(deep, _pOwner);
}
void AbstractNode::normalize()
{
}
bool AbstractNode::isSupported(const XMLString& feature, const XMLString& version) const
{
return DOMImplementation::instance().hasFeature(feature, version);
}
const XMLString& AbstractNode::namespaceURI() const
{
return EMPTY_STRING;
}
XMLString AbstractNode::prefix() const
{
return EMPTY_STRING;
}
const XMLString& AbstractNode::localName() const
{
return EMPTY_STRING;
}
bool AbstractNode::hasAttributes() const
{
return false;
}
XMLString AbstractNode::innerText() const
{
XMLString result = nodeValue();
Node* pChild = firstChild();
while (pChild)
{
result.append(pChild->innerText());
pChild = pChild->nextSibling();
}
return result;
}
void AbstractNode::addEventListener(const XMLString& type, EventListener* listener, bool useCapture)
{
if (_pEventDispatcher)
_pEventDispatcher->removeEventListener(type, listener, useCapture);
else
_pEventDispatcher = new EventDispatcher;
_pEventDispatcher->addEventListener(type, listener, useCapture);
}
void AbstractNode::removeEventListener(const XMLString& type, EventListener* listener, bool useCapture)
{
if (_pEventDispatcher)
_pEventDispatcher->removeEventListener(type, listener, useCapture);
}
bool AbstractNode::dispatchEvent(Event* evt)
{
if (eventsSuspended()) return true;
if (evt->type().empty()) throw EventException(EventException::UNSPECIFIED_EVENT_TYPE_ERR);
evt->setTarget(this);
evt->setCurrentPhase(Event::CAPTURING_PHASE);
if (_pParent) _pParent->captureEvent(evt);
if (_pEventDispatcher && !evt->isStopped())
{
evt->setCurrentPhase(Event::AT_TARGET);
evt->setCurrentTarget(this);
_pEventDispatcher->dispatchEvent(evt);
}
if (!evt->isStopped() && evt->bubbles() && _pParent)
{
evt->setCurrentPhase(Event::BUBBLING_PHASE);
_pParent->bubbleEvent(evt);
}
return evt->isCanceled();
}
void AbstractNode::captureEvent(Event* evt)
{
if (_pParent)
_pParent->captureEvent(evt);
if (_pEventDispatcher && !evt->isStopped())
{
evt->setCurrentTarget(this);
_pEventDispatcher->captureEvent(evt);
}
}
void AbstractNode::bubbleEvent(Event* evt)
{
evt->setCurrentTarget(this);
if (_pEventDispatcher)
{
_pEventDispatcher->bubbleEvent(evt);
}
if (_pParent && !evt->isStopped())
_pParent->bubbleEvent(evt);
}
void AbstractNode::dispatchSubtreeModified()
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMSubtreeModified, this, true, false, 0);
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchNodeInserted()
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMNodeInserted, this, true, false, parentNode());
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchNodeRemoved()
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMNodeRemoved, this, true, false, parentNode());
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchNodeRemovedFromDocument()
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMNodeRemovedFromDocument, this, false, false, 0);
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchNodeInsertedIntoDocument()
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMNodeInsertedIntoDocument, this, false, false, 0);
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchAttrModified(Attr* pAttr, MutationEvent::AttrChangeType changeType, const XMLString& prevValue, const XMLString& newValue)
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMAttrModified, this, true, false, pAttr, prevValue, newValue, pAttr->name(), changeType);
dispatchEvent(pEvent.get());
}
void AbstractNode::dispatchCharacterDataModified(const XMLString& prevValue, const XMLString& newValue)
{
AutoPtr<MutationEvent> pEvent = new MutationEvent(_pOwner, MutationEvent::DOMCharacterDataModified, this, true, false, 0, prevValue, newValue, EMPTY_STRING, MutationEvent::MODIFICATION);
dispatchEvent(pEvent.get());
}
bool AbstractNode::events() const
{
return _pOwner->events();
}
bool AbstractNode::eventsSuspended() const
{
return _pOwner->eventsSuspended();
}
void AbstractNode::setOwnerDocument(Document* pOwnerDocument)
{
_pOwner = pOwnerDocument;
}
XML_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -