domrangeimpl.cpp

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

CPP
2,115
字号
/* * 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: DOMRangeImpl.cpp,v 1.19 2004/09/08 13:55:52 peiyongz Exp $ */#include "DOMRangeImpl.hpp"#include "DOMDocumentImpl.hpp"#include "DOMDocumentFragmentImpl.hpp"#include "DOMCommentImpl.hpp"#include "DOMProcessingInstructionImpl.hpp"#include "DOMCasts.hpp"#include <xercesc/dom/DOMException.hpp>#include <xercesc/dom/DOMDocument.hpp>#include <xercesc/dom/DOMRangeException.hpp>#include <xercesc/dom/DOMText.hpp>#include <xercesc/dom/DOMProcessingInstruction.hpp>#include <xercesc/framework/XMLBuffer.hpp>#include <xercesc/util/Janitor.hpp>XERCES_CPP_NAMESPACE_BEGIN//---------------------// C'tor and D'tor//---------------------DOMRangeImpl::DOMRangeImpl(DOMDocument* doc, MemoryManager* const manager)    :   fStartContainer(doc),             fStartOffset(0),        fEndContainer(doc),        fEndOffset(0),                fCollapsed(true),        fDocument(doc),           fDetached(false),        fRemoveChild(0),        fMemoryManager(manager){}DOMRangeImpl::DOMRangeImpl(const DOMRangeImpl& other):   fStartContainer(other.fStartContainer),    fStartOffset(other.fStartOffset),    fEndContainer(other.fEndContainer),    fEndOffset(other.fEndOffset),    fCollapsed(other.fCollapsed),    fDocument(other.fDocument),    fDetached(other.fDetached),    fRemoveChild(other.fRemoveChild),    fMemoryManager(other.fMemoryManager){}DOMRangeImpl::~DOMRangeImpl(){}//-------------------------------// Public getter functions//-------------------------------DOMNode* DOMRangeImpl::getStartContainer() const{    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    return fStartContainer;}XMLSize_t DOMRangeImpl::getStartOffset() const{    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    return fStartOffset;}DOMNode* DOMRangeImpl::getEndContainer() const{    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    return fEndContainer;}XMLSize_t DOMRangeImpl::getEndOffset() const{    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    return fEndOffset;}bool DOMRangeImpl::getCollapsed() const{    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    return ((fStartContainer == fEndContainer)             && (fStartOffset == fEndOffset));}//-------------------------------// Public setter functions//-------------------------------void DOMRangeImpl::setStartContainer(const DOMNode* node){    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    fStartContainer = (DOMNode*) node;}void DOMRangeImpl::setStartOffset(XMLSize_t offset){    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    fStartOffset = offset;}void DOMRangeImpl::setEndContainer(const DOMNode* node){    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    fEndContainer = (DOMNode*) node;}void DOMRangeImpl::setEndOffset(XMLSize_t offset){    if (fDetached)    {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    fEndOffset = offset;}void DOMRangeImpl::setStart(const DOMNode* refNode, XMLSize_t offset){    validateNode(refNode);    checkIndex(refNode, offset);    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(true); //collapse the range positions to start            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fStartContainer = (DOMNode*) refNode;    fStartOffset    = offset;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fEndContainer))        collapse(true);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void DOMRangeImpl::setEnd(const DOMNode* refNode, XMLSize_t offset){    validateNode(refNode);    checkIndex(refNode, offset);    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(false); //collapse the range positions to end            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fEndContainer   = (DOMNode*) refNode;    fEndOffset      = offset;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fStartContainer))        collapse(false);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}void DOMRangeImpl::setStartBefore(const DOMNode* refNode){    if( fDetached) {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOMRangeException(            DOMRangeException::INVALID_NODE_TYPE_ERR, 0, fMemoryManager);    }    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(true); //collapse the range positions to start            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fStartContainer = refNode->getParentNode();   XMLSize_t i = 0;    for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling()) {        i++;    }    if (i == 0)        fStartOffset = 0;    else        fStartOffset = i-1;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fEndContainer))        collapse(true);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void DOMRangeImpl::setStartAfter(const DOMNode* refNode){    if( fDetached) {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOMRangeException(            DOMRangeException::INVALID_NODE_TYPE_ERR, 0, fMemoryManager);    }    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(true); //collapse the range positions to start            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fStartContainer = refNode->getParentNode();    XMLSize_t i = 0;    for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling()) {        i++;    }    fStartOffset = i;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fEndContainer))        collapse(true);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void DOMRangeImpl::setEndBefore(const DOMNode* refNode){    if( fDetached) {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOMRangeException(            DOMRangeException::INVALID_NODE_TYPE_ERR, 0, fMemoryManager);    }    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(false); //collapse the range positions to end            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fEndContainer = refNode->getParentNode();    XMLSize_t i = 0;    for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling(), i++) ;    if (i< 1)        fEndOffset = 0;    else        fEndOffset = i-1;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fStartContainer))        collapse(false);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}void DOMRangeImpl::setEndAfter(const DOMNode* refNode){    if( fDetached) {        throw DOMException(            DOMException::INVALID_STATE_ERR, 0, fMemoryManager);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOMRangeException(            DOMRangeException::INVALID_NODE_TYPE_ERR, 0, fMemoryManager);    }    // error if not the same owner document    if (fDocument != refNode->getOwnerDocument()) {        if ( refNode != fDocument ) {            collapse(false); //collapse the range positions to end            fCollapsed = true;            throw DOMException(                DOMException::WRONG_DOCUMENT_ERR, 0, fMemoryManager);        }    }    fEndContainer = refNode->getParentNode();    XMLSize_t i = 0;    for (DOMNode* n = (DOMNode*) refNode; n!=0; n = n->getPreviousSibling(), i++) ;    if (i ==0)        fEndOffset = 0;    else        fEndOffset = i;    // they may be of same document, but not same root container    // collapse if not the same root container    if (!commonAncestorOf(refNode, fStartContainer))        collapse(false);    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOMRange::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}//-------------------------------// Public Misc. functions//-------------------------------

⌨️ 快捷键说明

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