domdocumenttypeimpl.cpp

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

CPP
539
字号
/* * Copyright 2001-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: DOMDocumentTypeImpl.cpp,v 1.26 2004/09/08 13:55:51 peiyongz Exp $ */#include "DOMDocumentTypeImpl.hpp"#include <xercesc/dom/DOMNode.hpp>#include <xercesc/dom/DOMException.hpp>#include <xercesc/dom/DOMImplementationRegistry.hpp>#include <xercesc/dom/DOMImplementation.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLChar.hpp>#include <xercesc/util/XMLRegisterCleanup.hpp>#include "DOMNamedNodeMapImpl.hpp"#include "DOMDocumentImpl.hpp"#include "DOMCasts.hpp"XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  Local static data// ---------------------------------------------------------------------------static DOMDocument*       sDocument = 0;static XMLRegisterCleanup documentCleanup;static void reinitDocument(){    if (sDocument) {        sDocument->release();        sDocument = 0;    }}static DOMDocument& gDocTypeDocument(){    if (!sDocument)    {        static const XMLCh gCoreStr[] = { chLatin_C, chLatin_o, chLatin_r, chLatin_e, chNull };        DOMImplementation* impl =  DOMImplementationRegistry::getDOMImplementation(gCoreStr);        DOMDocument* tmpDoc = impl->createDocument();                   // document type object (DTD).        if (XMLPlatformUtils::compareAndSwap((void**)&sDocument, tmpDoc, 0))        {            // Someone beat us to it, so let's clean up ours            delete tmpDoc;        }        else        {            documentCleanup.registerCleanup(reinitDocument);        }    }    return *sDocument;}DOMDocumentTypeImpl::DOMDocumentTypeImpl(DOMDocument *ownerDoc,                                   const XMLCh *dtName,                                   bool heap)    : fNode(ownerDoc),    fParent(ownerDoc),    fName(0),    fEntities(0),    fNotations(0),    fElements(0),    fPublicId(0),    fSystemId(0),    fInternalSubset(0),    fIntSubsetReading(false),    fIsCreatedFromHeap(heap){    if (ownerDoc) {        fName = ((DOMDocumentImpl *)ownerDoc)->getPooledString(dtName);        fEntities = new (ownerDoc) DOMNamedNodeMapImpl(this);        fNotations= new (ownerDoc) DOMNamedNodeMapImpl(this);        fElements = new (ownerDoc) DOMNamedNodeMapImpl(this);    }    else {        DOMDocument* doc = &gDocTypeDocument();        fName = ((DOMDocumentImpl *)doc)->getPooledString(dtName);        fEntities = new (doc) DOMNamedNodeMapImpl(this);        fNotations= new (doc) DOMNamedNodeMapImpl(this);        fElements = new (doc) DOMNamedNodeMapImpl(this);    }}//Introduced in DOM Level 2DOMDocumentTypeImpl::DOMDocumentTypeImpl(DOMDocument *ownerDoc,                                   const XMLCh *qualifiedName,                                   const XMLCh *pubId,                                   const XMLCh *sysId,                                   bool heap)    : fNode(ownerDoc),    fParent(ownerDoc),    fName(0),    fEntities(0),    fNotations(0),    fElements(0),    fPublicId(0),    fSystemId(0),    fInternalSubset(0),        fIntSubsetReading(false),            fIsCreatedFromHeap(heap){    int index = DOMDocumentImpl::indexofQualifiedName(qualifiedName);    if (index < 0)        throw DOMException(DOMException::NAMESPACE_ERR, 0, GetDOMNodeMemoryManager);    else if (index > 0)    {        // we have to make sure the qualifiedName has correct prefix and localName        // although we don't really to store them separately        XMLCh* newName;        XMLCh temp[4000];        if (index >= 3999)            newName = (XMLCh*) XMLPlatformUtils::fgMemoryManager->allocate            (                (XMLString::stringLen(qualifiedName)+1) * sizeof(XMLCh)            );//new XMLCh[XMLString::stringLen(qualifiedName)+1];        else            newName = temp;        XMLString::copyNString(newName, qualifiedName, index);        newName[index] = chNull;        // Before we carry on, we should check if the prefix or localName are valid XMLName        if (ownerDoc) {            if (!((DOMDocumentImpl*)ownerDoc)->isXMLName(newName) || !((DOMDocumentImpl*)ownerDoc)->isXMLName(qualifiedName+index+1))                throw DOMException(DOMException::NAMESPACE_ERR, 0, GetDOMNodeMemoryManager);        }        else {            // document is not there yet, so assume XML 1.0            if (!XMLChar1_0::isValidName(newName, index) || !XMLChar1_0::isValidName(qualifiedName+index+1, XMLString::stringLen(qualifiedName)-index-1))                throw DOMException(DOMException::NAMESPACE_ERR, 0, GetDOMNodeMemoryManager);        }        if (index >= 3999)            XMLPlatformUtils::fgMemoryManager->deallocate(newName);//delete[] newName;    }    if (ownerDoc) {        DOMDocumentImpl *docImpl = (DOMDocumentImpl *)ownerDoc;        fPublicId = docImpl->cloneString(pubId);        fSystemId = docImpl->cloneString(sysId);        fName = ((DOMDocumentImpl *)ownerDoc)->getPooledString(qualifiedName);        fEntities = new (ownerDoc) DOMNamedNodeMapImpl(this);        fNotations= new (ownerDoc) DOMNamedNodeMapImpl(this);        fElements = new (ownerDoc) DOMNamedNodeMapImpl(this);    }    else {        DOMDocument* doc = &gDocTypeDocument();        fPublicId = ((DOMDocumentImpl*) doc)->cloneString(pubId);        fSystemId = ((DOMDocumentImpl*) doc)->cloneString(sysId);        fName = ((DOMDocumentImpl*) doc)->getPooledString(qualifiedName);        fEntities = new (doc) DOMNamedNodeMapImpl(this);        fNotations= new (doc) DOMNamedNodeMapImpl(this);        fElements = new (doc) DOMNamedNodeMapImpl(this);    }}DOMDocumentTypeImpl::DOMDocumentTypeImpl(const DOMDocumentTypeImpl &other, bool heap, bool deep)    : fNode(other.fNode),    fParent(other.fParent),    fChild(other.fChild),    fName(0),    fEntities(0),    fNotations(0),    fElements(0),    fPublicId(0),    fSystemId(0),    fInternalSubset(0),    fIntSubsetReading(other.fIntSubsetReading),           fIsCreatedFromHeap(heap){    fName = other.fName;    //DOM Level 2    fPublicId        = other.fPublicId;    fSystemId        = other.fSystemId;    fInternalSubset  = other.fInternalSubset;    if ((DOMDocumentImpl *)this->fNode.getOwnerDocument() && deep)        fParent.cloneChildren(&other);    fEntities = other.fEntities->cloneMap(this);    fNotations= other.fNotations->cloneMap(this);    fElements = other.fElements->cloneMap(this);}DOMDocumentTypeImpl::~DOMDocumentTypeImpl(){}DOMNode *DOMDocumentTypeImpl::cloneNode(bool deep) const{    DOMNode* newNode = 0;    if (castToNodeImpl(this)->getOwnerDocument())        newNode = new (castToNodeImpl(this)->getOwnerDocument(), DOMDocumentImpl::DOCUMENT_TYPE_OBJECT) DOMDocumentTypeImpl(*this, false, deep);    else        newNode = new (&gDocTypeDocument(), DOMDocumentImpl::DOCUMENT_TYPE_OBJECT) DOMDocumentTypeImpl(*this, false, deep);    fNode.callUserDataHandlers(DOMUserDataHandler::NODE_CLONED, this, newNode);    return newNode;}/** * NON-DOM * set the ownerDocument of this node and its children */void DOMDocumentTypeImpl::setOwnerDocument(DOMDocument *doc) {    if (castToNodeImpl(this)->getOwnerDocument()) {        fNode.setOwnerDocument(doc);        fParent.setOwnerDocument(doc);    }    else {        if (doc) {            DOMDocumentImpl *docImpl = (DOMDocumentImpl *)doc;            fPublicId = docImpl->cloneString(fPublicId);            fSystemId = docImpl->cloneString(fSystemId);            fInternalSubset = docImpl->cloneString(fInternalSubset);            fName = docImpl->getPooledString(fName);                        fNode.setOwnerDocument(doc);            fParent.setOwnerDocument(doc);            DOMNamedNodeMapImpl* entitiesTemp = fEntities->cloneMap(this);            DOMNamedNodeMapImpl* notationsTemp = fNotations->cloneMap(this);            DOMNamedNodeMapImpl* elementsTemp = fElements->cloneMap(this);            fEntities = entitiesTemp;            fNotations = notationsTemp;            fElements = elementsTemp;        }    }}const XMLCh * DOMDocumentTypeImpl::getNodeName() const{    return fName;}short DOMDocumentTypeImpl::getNodeType()  const {    return DOMNode::DOCUMENT_TYPE_NODE;}DOMNamedNodeMap *DOMDocumentTypeImpl::getEntities() const

⌨️ 快捷键说明

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