rangeimpl.cpp

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

CPP
1,660
字号
/* * 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: RangeImpl.cpp,v 1.6 2004/09/23 00:50:49 cargilld Exp $ */#include <xercesc/util/RefVectorOf.hpp>#include "NodeImpl.hpp"#include "RangeImpl.hpp"#include "TextImpl.hpp"#include "DocumentImpl.hpp"#include "DOM_DOMException.hpp"#include "DOM_Document.hpp"#include "DocumentFragmentImpl.hpp"#include "DOM_Document.hpp"#include "DOM_RangeException.hpp"#include "DOM_DOMException.hpp"#include "DOM_Text.hpp"XERCES_CPP_NAMESPACE_BEGIN//---------------------// C'tor and D'tor//---------------------RangeImpl::RangeImpl(DOM_Document doc)    :   fDocument(doc),        fStartContainer(doc),        fStartOffset(0),        fEndContainer(doc),        fEndOffset(0),        fDetached(false),        fCollapsed(true),        fRemoveChild(0){}RangeImpl::RangeImpl(const RangeImpl& other){    fDocument = other.fDocument;    fStartContainer = other.fStartContainer;    fStartOffset = other.fStartOffset;    fEndContainer = other.fEndContainer;    fEndOffset = other.fEndOffset;    fDetached = other.fDetached;    fCollapsed = other.fCollapsed;    fRemoveChild = other.fRemoveChild;}RangeImpl::~RangeImpl(){}void RangeImpl::unreferenced(){    if (((DocumentImpl*)fDocument.fImpl)->ranges != 0L) {        int sz = ((DocumentImpl*)fDocument.fImpl)->ranges->size();        for (int i=0; i< sz; i++) {            if (((DocumentImpl*)fDocument.fImpl)->ranges->elementAt(i) == this) {                ((DocumentImpl*)fDocument.fImpl)->ranges->removeElementAt(i);                break;            }        }    }//    delete this;    RangeImpl* ptr = this;    delete ptr;};//-------------------------------// Public getter functions//-------------------------------DOM_Node RangeImpl::getStartContainer() const{    return fStartContainer;}unsigned int RangeImpl::getStartOffset() const{    return fStartOffset;}DOM_Node RangeImpl::getEndContainer() const{    return fEndContainer;}unsigned int RangeImpl::getEndOffset() const{    return fEndOffset;}bool RangeImpl::getCollapsed() const{    if (fDetached)    {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    return ((fStartContainer == fEndContainer)             && (fStartOffset == fEndOffset));}//-------------------------------// Public getter functions//-------------------------------void RangeImpl::setStartContainer(const DOM_Node& node){    if (fDetached)    {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    fStartContainer = node;}void RangeImpl::setStartOffset(unsigned int offset){    if (fDetached)    {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    fStartOffset = offset;}void RangeImpl::setEndContainer(const DOM_Node& node){    if (fDetached)    {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    fEndContainer = node;}void RangeImpl::setEndOffset(unsigned int offset){    if (fDetached)    {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    fEndOffset = offset;}void RangeImpl::setStart(const DOM_Node& refNode, unsigned int offset){    validateNode(refNode);    checkIndex(refNode, offset);    fStartContainer = refNode;    fStartOffset    = offset;    if ((fDocument != refNode.getOwnerDocument() )        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(true);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void RangeImpl::setEnd(const DOM_Node& refNode, unsigned int offset){    validateNode(refNode);    checkIndex(refNode, offset);    fEndContainer   = refNode;    fEndOffset      = offset;    if ((fDocument != refNode.getOwnerDocument() )        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(false);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}void RangeImpl::setStartBefore(const DOM_Node& refNode){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOM_RangeException(            DOM_RangeException::INVALID_NODE_TYPE_ERR, null);    }    fStartContainer = refNode.getParentNode();   unsigned int i = 0;    for (DOM_Node n = refNode; n!=null; n = n.getPreviousSibling()) {        i++;    }    if (i == 0)        fStartOffset = 0;    else        fStartOffset = i-1;    if ((fDocument != refNode.getOwnerDocument())        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(true);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void RangeImpl::setStartAfter(const DOM_Node& refNode){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOM_RangeException(            DOM_RangeException::INVALID_NODE_TYPE_ERR, null);    }    fStartContainer = refNode.getParentNode();    unsigned int i = 0;    for (DOM_Node n = refNode; n!=null; n = n.getPreviousSibling()) {        i++;    }    fStartOffset = i;    if ((fDocument != refNode.getOwnerDocument() )        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(true);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(true); //collapse the range positions to start    else        fCollapsed = false;}void RangeImpl::setEndBefore(const DOM_Node& refNode){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOM_RangeException(            DOM_RangeException::INVALID_NODE_TYPE_ERR, null);    }    fEndContainer = refNode.getParentNode();    unsigned int i = 0;    for (DOM_Node n = refNode; n!=null; n = n.getPreviousSibling(), i++) ;    if (i< 1)        fEndOffset = 0;    else        fEndOffset = i-1;    if ((fDocument != refNode.getOwnerDocument() )        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(true);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}void RangeImpl::setEndAfter(const DOM_Node& refNode){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) {        throw DOM_RangeException(            DOM_RangeException::INVALID_NODE_TYPE_ERR, null);    }    fEndContainer = refNode.getParentNode();    unsigned int i = 0;    for (DOM_Node n = refNode; n!=null; n = n.getPreviousSibling(), i++) ;    if (i ==0)        fEndOffset = 0;    else        fEndOffset = i;    if ((fDocument != refNode.getOwnerDocument() )        && (refNode.getOwnerDocument().fImpl != 0) )    {        fDocument = refNode.getOwnerDocument();        collapse(true);    }    //compare the start and end boundary point    //collapse if start point is after the end point    if(compareBoundaryPoints(DOM_Range::END_TO_START, this) == 1)        collapse(false); //collapse the range positions to end    else        fCollapsed = false;}//-------------------------------// Public Misc. functions//-------------------------------void RangeImpl::detach(){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    fDetached = true;    //nullify nodes    fStartContainer = 0;    fStartOffset    = 0;    fEndContainer   = 0;    fEndOffset      = 0;    fCollapsed      = true;    fRemoveChild    = 0;}void RangeImpl::collapse(bool toStart){    if( fDetached) {        throw DOM_DOMException(            DOM_DOMException::INVALID_STATE_ERR, null);    }    if (toStart) {        fEndContainer = fStartContainer;        fEndOffset = fStartOffset;    } else {        fStartContainer = fEndContainer;        fStartOffset = fEndOffset;    }    fCollapsed = true;}void RangeImpl::selectNode(const DOM_Node& refNode){    validateNode(refNode);    if ( !isLegalContainedNode(refNode)) {        throw DOM_RangeException(            DOM_RangeException::INVALID_NODE_TYPE_ERR, null);    }    //First check for the text type node    if (refNode.getNodeType() ==  DOM_Node::TEXT_NODE)    {        //The node itself is the container.        fStartContainer = refNode;        fEndContainer   = refNode;        //Select all the contents of the node        fStartOffset = 0;        fEndOffset = ((DOM_Text &)refNode).getLength();

⌨️ 快捷键说明

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