documentimpl.cpp

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

CPP
845
字号
/* * Copyright 1999-2002,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: DocumentImpl.cpp,v 1.10 2004/09/23 00:50:49 cargilld Exp $ *///// file DocumentImpl.cpp//#include "DocumentImpl.hpp"#include "DOM_DOMException.hpp"#include "DOM_Node.hpp"#include "DocumentTypeImpl.hpp"#include "ElementImpl.hpp"#include "ElementNSImpl.hpp"#include "AttrImpl.hpp"#include "AttrNSImpl.hpp"#include "CDATASectionImpl.hpp"#include "CommentImpl.hpp"#include "DocumentFragmentImpl.hpp"#include "EntityImpl.hpp"#include "EntityReferenceImpl.hpp"#include "NotationImpl.hpp"#include "ProcessingInstructionImpl.hpp"#include "TextImpl.hpp"#include "DOM_DOMImplementation.hpp"#include "DeepNodeListImpl.hpp"#include "NamedNodeMapImpl.hpp"#include "DStringPool.hpp"#include <xercesc/internal/XMLReader.hpp>#include "TreeWalkerImpl.hpp"#include "NodeIteratorImpl.hpp"#include "NodeIDMap.hpp"#include "DOM_Document.hpp"#include <xercesc/util/HashPtr.hpp>#include "RangeImpl.hpp"#include "DOM_Document.hpp"XERCES_CPP_NAMESPACE_BEGINstatic DOMString *nam = 0;  // will be lazily initialized to "#document"static XMLRegisterCleanup namCleanup;DocumentImpl::DocumentImpl(MemoryManager* const manager)    : ParentNode(this)    , docType(0)    , docElement(0)    , namePool(0)    , fNodeIDMap(0)    , iterators(0)    , treeWalkers(0)    , userData(0)    , ranges(0)    , fChanges(0)    , errorChecking(true)    , fMemoryManager(manager){    namePool    = new (fMemoryManager) DStringPool(257, fMemoryManager);};//DOM Level 2DocumentImpl::DocumentImpl(const DOMString &fNamespaceURI,                           const DOMString &qualifiedName,                           DocumentTypeImpl *doctype,                           MemoryManager* const manager)    : ParentNode(this)    , docType(0)    , docElement(0)    , namePool(0)    , fNodeIDMap(0)    , iterators(0)    , treeWalkers(0)    , userData(0)    , ranges(0)    , fChanges(0)    , errorChecking(true)    , fMemoryManager(manager){	setDocumentType(doctype);    namePool    = new (fMemoryManager) DStringPool(257, fMemoryManager);    appendChild(createElementNS(fNamespaceURI, qualifiedName));  //root element}void DocumentImpl::setDocumentType(DocumentTypeImpl *doctype){	if (!doctype)		return;	if (doctype->getOwnerDocument() != null)        throw DOM_DOMException(	//one doctype can belong to only one DocumentImpl        DOM_DOMException::WRONG_DOCUMENT_ERR, null);    doctype->setOwnerDocument(this);    doctype->getEntities()->ownerNode->setOwnerDocument(this);    doctype->getNotations()->ownerNode->setOwnerDocument(this);    doctype -> referenced();         // Warning, tricky!  An external (DOM_Node) reference                                     //  to a node normally bumps the reference count to its                                     //  document also.  But this could not happen when the                                     //  user created the DOM_DocumentType because there was                                     //  no document yet.  Now we have the document, and                                     //  the docs ref count must be got back in sync.					appendChild(doctype);}DocumentImpl::~DocumentImpl(){    if (iterators != 0L) {        // The data in the vector is pointers owned by smart pointers, and will be cleaned up when they go away.        delete iterators;    }    if (treeWalkers != 0L) {        // The data in the vector is pointers owned by smart pointers, and will be cleaned up when they go away.        delete treeWalkers;    }    if (ranges != 0L) {        delete ranges;        ranges = 0;    }    if (userData) {        // make sure we won't access userData any further        hasUserData(false);        delete userData;    }    delete namePool;    // Do not delete docType and docElement pointers here.    //  These are also normal child nodes of the document,    //  and refcounting will take them out in the usual way.    delete fNodeIDMap;};NodeImpl *DocumentImpl::cloneNode(bool deep) {    // clone the node itself    DocumentImpl *newdoc = new (fMemoryManager) DocumentImpl(fMemoryManager);    // then the children by _importing_ them    if (deep) {        for (ChildNode *n = firstChild; n != null; n = n->nextSibling) {            newdoc->appendChild(newdoc->importNode(n, true));	}    }    newdoc->setErrorChecking(errorChecking);    return newdoc;};DOMString DocumentImpl::getNodeName() {    return DStringPool::getStaticString("#document"                                       , &nam                                       , reinitDocumentImpl                                       , namCleanup);}short DocumentImpl::getNodeType() {    return DOM_Node::DOCUMENT_NODE;};// even though ownerDocument refers to this in this implementation// the DOM Level 2 spec says it must be null, so make it appear soDocumentImpl * DocumentImpl::getOwnerDocument() {    return null;}bool DocumentImpl::isDocumentImpl() {    return true;};AttrImpl *DocumentImpl::createAttribute(const DOMString &nam){    if (errorChecking && !isXMLName(nam)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR,null);    }    return new (fMemoryManager) AttrImpl(this,nam);};CDATASectionImpl *DocumentImpl::createCDATASection(const DOMString &data) {    return new (fMemoryManager) CDATASectionImpl(this,data);};CommentImpl *DocumentImpl::createComment(const DOMString &data){    return new (fMemoryManager) CommentImpl(this,data);};DocumentFragmentImpl *DocumentImpl::createDocumentFragment(){    return new (fMemoryManager) DocumentFragmentImpl(this);};DocumentTypeImpl *DocumentImpl::createDocumentType(const DOMString &nam){    if (errorChecking && !isXMLName(nam)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR, null);    }    return new (fMemoryManager) DocumentTypeImpl(this, nam);};DocumentTypeImpl *    DocumentImpl::createDocumentType(const DOMString &qualifiedName,                                     const DOMString &publicId,                                     const DOMString &systemId){    if (errorChecking && !isXMLName(qualifiedName)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR, null);    }    return new (fMemoryManager) DocumentTypeImpl(this, qualifiedName, publicId, systemId);};ElementImpl *DocumentImpl::createElement(const DOMString &tagName){    if (errorChecking && !isXMLName(tagName)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR,null);    }    DOMString pooledTagName = this->namePool->getPooledString(tagName);    return new (fMemoryManager) ElementImpl(this,pooledTagName);};ElementImpl *DocumentImpl::createElement(const XMLCh *tagName){    DOMString pooledTagName = this->namePool->getPooledString(tagName);    return new (fMemoryManager) ElementImpl(this,pooledTagName);};EntityImpl *DocumentImpl::createEntity(const DOMString &nam){    if (errorChecking && !isXMLName(nam)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR, null);    }    return new (fMemoryManager) EntityImpl(this, nam);};EntityReferenceImpl *DocumentImpl::createEntityReference(const DOMString &nam){    if (errorChecking && !isXMLName(nam)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR, null);    }    return new (fMemoryManager) EntityReferenceImpl(this, nam);};NotationImpl *DocumentImpl::createNotation(const DOMString &nam){    if (errorChecking && !isXMLName(nam)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR, null);    }    return new (fMemoryManager) NotationImpl(this, nam);};ProcessingInstructionImpl *DocumentImpl::createProcessingInstruction(                                          const DOMString &target, const DOMString &data){    if (errorChecking && !isXMLName(target)) {        throw DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR,null);    }    return new (fMemoryManager) ProcessingInstructionImpl(this,target,data);};TextImpl *DocumentImpl::createTextNode(const DOMString &data){    return new (fMemoryManager) TextImpl(this,data);};NodeIteratorImpl* DocumentImpl::createNodeIterator (DOM_Node root,                                                    unsigned long whatToShow,                                                    DOM_NodeFilter* filter,                                                    bool entityReferenceExpansion,                                                    MemoryManager* const manager){		// Create the node iterator implementation object.		//	Add it to the vector of iterators that must be synchronized when a node is deleted.		//	The vector of iterators is kept in the "owner document" if there is one. If there isn't one, I assume that root is the		//	owner document.    NodeIteratorImpl* iter = new (manager) NodeIteratorImpl(root, whatToShow, filter, entityReferenceExpansion);    DOM_Document doc = root.getOwnerDocument();    DocumentImpl* impl;    if (! doc.isNull()) {        impl = (DocumentImpl *) doc.fImpl;    }    else        impl = (DocumentImpl *) root.fImpl;    if (impl->iterators == 0L) {        impl->iterators = new (manager) NodeIterators(1, false, manager);        impl->iterators->addElement(iter);    }    return iter;}TreeWalkerImpl* DocumentImpl::createTreeWalker (DOM_Node root, unsigned long whatToShow,                                                DOM_NodeFilter* filter,                                                bool entityReferenceExpansion,                                                MemoryManager* const manager){		// See notes for createNodeIterator...    TreeWalkerImpl* twi = new (manager) TreeWalkerImpl(root, whatToShow, filter, entityReferenceExpansion);    DOM_Document doc = root.getOwnerDocument();    DocumentImpl* impl;    if (! doc.isNull()) {        impl = (DocumentImpl *) doc.fImpl;    }    else        impl = (DocumentImpl *) root.fImpl;    if (impl->treeWalkers == 0L) {        impl->treeWalkers = new (manager) TreeWalkers(1, false, manager);        impl->treeWalkers->addElement(twi);    }    return twi;}DocumentTypeImpl *DocumentImpl::getDoctype(){    return docType;};ElementImpl *DocumentImpl::getDocumentElement(){    return docElement;};DeepNodeListImpl *DocumentImpl::getElementsByTagName(const DOMString &tagname){    return new (fMemoryManager) DeepNodeListImpl(this,tagname);};NodeImpl *DocumentImpl::insertBefore(NodeImpl *newChild, NodeImpl *refChild){    // Only one such child permitted    if (errorChecking &&        ((newChild->isElementImpl() && docElement!=null) ||         (newChild->isDocumentTypeImpl() && docType!=null))) {        throw DOM_DOMException(DOM_DOMException::HIERARCHY_REQUEST_ERR,null);    }    ParentNode::insertBefore(newChild,refChild);    // If insert succeeded, cache the kid appropriately    if(newChild->isElementImpl())        docElement=(ElementImpl *)newChild;    else if(newChild->isDocumentTypeImpl())        docType=(DocumentTypeImpl *)newChild;    return newChild;};bool DocumentImpl::isXMLName(const DOMString &s){    return XMLChar1_0::isValidName(s.rawBuffer(),s.length());};

⌨️ 快捷键说明

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