📄 document.cpp
字号:
//
// Document.cpp
//
// $Id: //poco/Main/XML/src/Document.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/Document.h"
#include "DOM/DocumentType.h"
#include "DOM/DOMImplementation.h"
#include "DOM/Element.h"
#include "DOM/Attr.h"
#include "DOM/DocumentFragment.h"
#include "DOM/Text.h"
#include "DOM/Comment.h"
#include "DOM/CDATASection.h"
#include "DOM/ProcessingInstruction.h"
#include "DOM/EntityReference.h"
#include "DOM/DOMException.h"
#include "DOM/ElementsByTagNameList.h"
#include "DOM/Entity.h"
#include "DOM/Notation.h"
#include "XML/Name.h"
#include "XML/NamePool.h"
XML_BEGIN
const XMLString Document::NODE_NAME = toXMLString("#document");
Document::Document(NamePool* pNamePool):
AbstractContainerNode(0),
_pDocumentType(0),
_eventSuspendLevel(0)
{
if (pNamePool)
{
_pNamePool = pNamePool;
_pNamePool->duplicate();
}
else
{
_pNamePool = new NamePool;
}
}
Document::Document(DocumentType* pDocumentType, NamePool* pNamePool):
AbstractContainerNode(0),
_pDocumentType(pDocumentType),
_eventSuspendLevel(0)
{
if (pNamePool)
{
_pNamePool = pNamePool;
_pNamePool->duplicate();
}
else
{
_pNamePool = new NamePool;
}
if (_pDocumentType)
{
_pDocumentType->duplicate();
_pDocumentType->setOwnerDocument(this);
}
}
Document::~Document()
{
if (_pDocumentType) _pDocumentType->release();
_pNamePool->release();
}
bool Document::dispatchEvent(Event* evt)
{
return _eventSuspendLevel > 0 || AbstractContainerNode::dispatchEvent(evt);
}
void Document::collectGarbage()
{
_autoReleasePool.release();
}
void Document::suspendEvents()
{
++_eventSuspendLevel;
}
void Document::resumeEvents()
{
poco_assert_dbg (_eventSuspendLevel > 0);
--_eventSuspendLevel;
}
const DOMImplementation& Document::implementation() const
{
return DOMImplementation::instance();
}
Element* Document::documentElement() const
{
// Skip non-element nodes before the document element
Node* pCur = firstChild();
while (pCur)
{
if (dynamic_cast<Element*>(pCur))
return static_cast<Element*>(pCur);
pCur = pCur->nextSibling();
}
return 0;
}
Element* Document::createElement(const XMLString& tagName) const
{
return new Element(const_cast<Document*>(this), EMPTY_STRING, EMPTY_STRING, tagName);
}
DocumentFragment* Document::createDocumentFragment() const
{
return new DocumentFragment(const_cast<Document*>(this));
}
Text* Document::createTextNode(const XMLString& data) const
{
return new Text(const_cast<Document*>(this), data);
}
Comment* Document::createComment(const XMLString& data) const
{
return new Comment(const_cast<Document*>(this), data);
}
CDATASection* Document::createCDATASection(const XMLString& data) const
{
return new CDATASection(const_cast<Document*>(this), data);
}
ProcessingInstruction* Document::createProcessingInstruction(const XMLString& target, const XMLString& data) const
{
return new ProcessingInstruction(const_cast<Document*>(this), target, data);
}
Attr* Document::createAttribute(const XMLString& name) const
{
return new Attr(const_cast<Document*>(this), 0, EMPTY_STRING, EMPTY_STRING, name, EMPTY_STRING);
}
EntityReference* Document::createEntityReference(const XMLString& name) const
{
return new EntityReference(const_cast<Document*>(this), name);
}
NodeList* Document::getElementsByTagName(const XMLString& name) const
{
return new ElementsByTagNameList(const_cast<Document*>(this), name);
}
const XMLString& Document::nodeName() const
{
return NODE_NAME;
}
unsigned short Document::nodeType() const
{
return Node::DOCUMENT_NODE;
}
Node* Document::importNode(Node* importedNode, bool deep)
{
return static_cast<AbstractNode*>(importedNode)->copyNode(deep, this);
}
Element* Document::createElementNS(const XMLString& namespaceURI, const XMLString& qualifiedName) const
{
return new Element(const_cast<Document*>(this), namespaceURI, Name::localName(qualifiedName), qualifiedName);
}
Attr* Document::createAttributeNS(const XMLString& namespaceURI, const XMLString& qualifiedName) const
{
return new Attr(const_cast<Document*>(this), 0, namespaceURI, Name::localName(qualifiedName), qualifiedName, EMPTY_STRING);
}
NodeList* Document::getElementsByTagNameNS(const XMLString& namespaceURI, const XMLString& localName) const
{
return new ElementsByTagNameListNS(const_cast<Document*>(this), namespaceURI, localName);
}
Element* Document::getElementById(const XMLString& elementId) const
{
return 0;
}
Event* Document::createEvent(const XMLString& eventType) const
{
if (eventType == MutationEvent::DOMSubtreeModified ||
eventType == MutationEvent::DOMNodeInserted ||
eventType == MutationEvent::DOMNodeRemoved ||
eventType == MutationEvent::DOMNodeRemovedFromDocument ||
eventType == MutationEvent::DOMNodeInsertedIntoDocument ||
eventType == MutationEvent::DOMAttrModified ||
eventType == MutationEvent::DOMCharacterDataModified)
{
return new MutationEvent(const_cast<Document*>(this), eventType);
}
throw DOMException(DOMException::NOT_SUPPORTED_ERR);
}
Node* Document::copyNode(bool deep, Document* pOwnerDocument) const
{
throw DOMException(DOMException::NOT_SUPPORTED_ERR);
}
void Document::setDoctype(DocumentType* pDoctype)
{
if (_pDocumentType) _pDocumentType->release();
_pDocumentType = pDoctype;
if (_pDocumentType)
{
_pDocumentType->duplicate();
_pDocumentType->setOwnerDocument(this);
}
}
bool Document::eventsSuspended() const
{
return _eventSuspendLevel > 0;
}
bool Document::events() const
{
return _eventSuspendLevel == 0;
}
Entity* Document::createEntity(const XMLString& name, const XMLString& publicId, const XMLString& systemId, const XMLString& notationName) const
{
return new Entity(const_cast<Document*>(this), name, publicId, systemId, notationName);
}
Notation* Document::createNotation(const XMLString& name, const XMLString& publicId, const XMLString& systemId) const
{
return new Notation(const_cast<Document*>(this), name, publicId, systemId);
}
XML_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -