domelementimpl.cpp

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

CPP
650
字号
/* * Copyright 2001-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: DOMElementImpl.cpp,v 1.28 2004/09/20 15:00:50 amassari Exp $ */#include "DOMElementImpl.hpp"#include <xercesc/dom/DOMAttr.hpp>#include <xercesc/dom/DOMDocument.hpp>#include <xercesc/dom/DOMException.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUri.hpp>#include "DOMAttrMapImpl.hpp"#include "DOMAttrImpl.hpp"#include "DOMDocumentImpl.hpp"#include "DOMParentNode.hpp"#include "DOMCasts.hpp"#include "DOMElementNSImpl.hpp"#include "DOMTypeInfoImpl.hpp"#include "DOMDocumentTypeImpl.hpp"#include <xercesc/util/OutOfMemoryException.hpp>XERCES_CPP_NAMESPACE_BEGINclass DOMAttr;DOMElementImpl::DOMElementImpl(DOMDocument *ownerDoc, const XMLCh *eName)    : fNode(ownerDoc), fParent(ownerDoc), fAttributes(0), fDefaultAttributes(0){    DOMDocumentImpl *docImpl = (DOMDocumentImpl *)ownerDoc;    fName = docImpl->getPooledString(eName);    setupDefaultAttributes();    if (!fDefaultAttributes) {        fDefaultAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this);        fAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this);    }    else {        fAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this, fDefaultAttributes);    }}DOMElementImpl::DOMElementImpl(const DOMElementImpl &other, bool deep)    : fNode(other.getOwnerDocument()),      fParent(other.getOwnerDocument()),      fAttributes(0),      fDefaultAttributes(0){    fName = other.fName;    if (deep)        fParent.cloneChildren(&other);    if (other.getAttributes())    {        fAttributes = ((DOMAttrMapImpl *)other.getAttributes())->cloneAttrMap(this);    }    if (other.getDefaultAttributes())    {        fDefaultAttributes = ((DOMAttrMapImpl *)other.getDefaultAttributes())->cloneAttrMap(this);    }    if (!fDefaultAttributes)        setupDefaultAttributes();    if (!fDefaultAttributes)        fDefaultAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this);    if (!fAttributes) {        if (!fDefaultAttributes) {            fAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this);        }        else {            fAttributes = new (getOwnerDocument()) DOMAttrMapImpl(this, fDefaultAttributes);        }    }}DOMElementImpl::~DOMElementImpl(){}DOMNode *DOMElementImpl::cloneNode(bool deep) const{    DOMNode* newNode = new (getOwnerDocument(), DOMDocumentImpl::ELEMENT_OBJECT) DOMElementImpl(*this, deep);    fNode.callUserDataHandlers(DOMUserDataHandler::NODE_CLONED, this, newNode);    return newNode;}const XMLCh * DOMElementImpl::getNodeName() const {    return fName;}short DOMElementImpl::getNodeType() const {    return DOMNode::ELEMENT_NODE;}const XMLCh * DOMElementImpl::getAttribute(const XMLCh *nam) const{    DOMNode * attr = fAttributes->getNamedItem(nam);    if (attr)        return attr->getNodeValue();    return XMLUni::fgZeroLenString;}DOMAttr *DOMElementImpl::getAttributeNode(const XMLCh *nam) const{    return  (DOMAttr *)fAttributes->getNamedItem(nam);}DOMNamedNodeMap *DOMElementImpl::getAttributes() const{    DOMElementImpl *ncThis = (DOMElementImpl *)this;   // cast off const    return ncThis->fAttributes;}DOMNodeList *DOMElementImpl::getElementsByTagName(const XMLCh *tagname) const{    DOMDocumentImpl *docImpl = (DOMDocumentImpl *)getOwnerDocument();    return docImpl->getDeepNodeList(this,tagname);}const XMLCh * DOMElementImpl::getTagName() const{    return fName;}void DOMElementImpl::removeAttribute(const XMLCh *nam){    if (fNode.isReadOnly())        throw DOMException(             DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    XMLSSize_t i = fAttributes->findNamePoint(nam);    if (i >= 0)    {        DOMNode *att = fAttributes->removeNamedItemAt(i);        ((DOMAttrImpl *)att)->removeAttrFromIDNodeMap();        att->release();    }}DOMAttr *DOMElementImpl::removeAttributeNode(DOMAttr *oldAttr){    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    DOMNode* found = 0;    // Since there is no removeAttributeNodeNS, check if this oldAttr has NS or not    const XMLCh* localName = oldAttr->getLocalName();    XMLSSize_t i = 0;    if (localName)        i = fAttributes->findNamePoint(oldAttr->getNamespaceURI(), localName);    else        i = fAttributes->findNamePoint(oldAttr->getName());    if (i >= 0) {        // If it is in fact the right object, remove it.        found = fAttributes->item(i);        if (found == oldAttr) {            fAttributes->removeNamedItemAt(i);            ((DOMAttrImpl *)oldAttr)->removeAttrFromIDNodeMap();        }        else            throw DOMException(DOMException::NOT_FOUND_ERR, 0, GetDOMNodeMemoryManager);    }    else        throw DOMException(DOMException::NOT_FOUND_ERR, 0, GetDOMNodeMemoryManager);   return (DOMAttr *)found;}void DOMElementImpl::setAttribute(const XMLCh *nam, const XMLCh *val){    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    DOMAttr* newAttr = getAttributeNode(nam);    if (!newAttr)    {        newAttr = this->fNode.getOwnerDocument()->createAttribute(nam);        fAttributes->setNamedItem(newAttr);    }    newAttr->setNodeValue(val);}void DOMElementImpl::setIdAttribute(const XMLCh* name){    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    DOMAttr *attr = getAttributeNode(name);    if (!attr)         throw DOMException(DOMException::NOT_FOUND_ERR, 0, GetDOMNodeMemoryManager);    ((DOMAttrImpl *)attr)->addAttrToIDNodeMap();}void DOMElementImpl::setIdAttributeNS(const XMLCh* namespaceURI, const XMLCh* localName) {    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    DOMAttr *attr = getAttributeNodeNS(namespaceURI, localName);    if (!attr)         throw DOMException(DOMException::NOT_FOUND_ERR, 0, GetDOMNodeMemoryManager);    ((DOMAttrImpl *)attr)->addAttrToIDNodeMap();}void DOMElementImpl::setIdAttributeNode(const DOMAttr *idAttr) {    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    DOMAttr *attr;    const XMLCh* localName = idAttr->getLocalName();    if (localName)        attr = getAttributeNodeNS(idAttr->getNamespaceURI(), idAttr->getLocalName());    else         attr = getAttributeNode(idAttr->getName());        if(!attr)         throw DOMException(DOMException::NOT_FOUND_ERR, 0, GetDOMNodeMemoryManager);    ((DOMAttrImpl *)attr)->addAttrToIDNodeMap();}DOMAttr * DOMElementImpl::setAttributeNode(DOMAttr *newAttr){    if (fNode.isReadOnly())        throw DOMException(        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMNodeMemoryManager);    if (newAttr->getNodeType() != DOMNode::ATTRIBUTE_NODE)        throw DOMException(DOMException::WRONG_DOCUMENT_ERR, 0, GetDOMNodeMemoryManager);        // revisit.  Exception doesn't match test.    // This will throw INUSE if necessary    DOMAttr *oldAttr = (DOMAttr *) fAttributes->setNamedItem(newAttr);    return oldAttr;}void DOMElementImpl::setNodeValue(const XMLCh *x){    fNode.setNodeValue(x);}void DOMElementImpl::setReadOnly(bool readOnl, bool deep){    fNode.setReadOnly(readOnl,deep);    fAttributes->setReadOnly(readOnl,true);}//Introduced in DOM Level 2const XMLCh * DOMElementImpl::getAttributeNS(const XMLCh *fNamespaceURI,    const XMLCh *fLocalName) const{    DOMAttr * attr=      (DOMAttr *)(fAttributes->getNamedItemNS(fNamespaceURI, fLocalName));    return (attr==0) ? XMLUni::fgZeroLenString : attr->getValue();}void DOMElementImpl::setAttributeNS(const XMLCh *fNamespaceURI,    const XMLCh *qualifiedName, const XMLCh *fValue){    if (fNode.isReadOnly())        throw DOMException(

⌨️ 快捷键说明

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