rangeimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,729 行 · 第 1/5 页
JAVA
1,729 行
/* * Copyright 1999-2005 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. */package com.sun.org.apache.xerces.internal.dom;import java.util.Vector;import org.w3c.dom.CharacterData;import org.w3c.dom.DOMException;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Node;import org.w3c.dom.ranges.Range;import org.w3c.dom.ranges.RangeException;/** The RangeImpl class implements the org.w3c.dom.range.Range interface. * <p> Please see the API documentation for the interface classes * and use the interfaces in your client programs. * * @xerces.internal * * @version $Id: RangeImpl.java,v 1.2.6.1 2005/08/31 12:44:48 sunithareddy Exp $ */public class RangeImpl implements Range { // // Constants // // // Data // DocumentImpl fDocument; Node fStartContainer; Node fEndContainer; int fStartOffset; int fEndOffset; boolean fIsCollapsed; boolean fDetach = false; Node fInsertNode = null; Node fDeleteNode = null; Node fSplitNode = null; // Was the Node inserted from the Range or the Document boolean fInsertedFromRange = false; /** The constructor. Clients must use DocumentRange.createRange(), * because it registers the Range with the document, so it can * be fixed-up. */ public RangeImpl(DocumentImpl document) { fDocument = document; fStartContainer = document; fEndContainer = document; fStartOffset = 0; fEndOffset = 0; fDetach = false; } public Node getStartContainer() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } return fStartContainer; } public int getStartOffset() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } return fStartOffset; } public Node getEndContainer() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } return fEndContainer; } public int getEndOffset() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } return fEndOffset; } public boolean getCollapsed() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } return (fStartContainer == fEndContainer && fStartOffset == fEndOffset); } public Node getCommonAncestorContainer() { if ( fDetach ) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } Vector startV = new Vector(); Node node; for (node=fStartContainer; node != null; node=node.getParentNode()) { startV.addElement(node); } Vector endV = new Vector(); for (node=fEndContainer; node != null; node=node.getParentNode()) { endV.addElement(node); } int s = startV.size()-1; int e = endV.size()-1; Object result = null; while (s>=0 && e>=0) { if (startV.elementAt(s) == endV.elementAt(e)) { result = startV.elementAt(s); } else { break; } --s; --e; } return (Node)result; } public void setStart(Node refNode, int offset) throws RangeException, DOMException { if (fDocument.errorChecking) { if ( fDetach) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if ( !isLegalContainer(refNode)) { throw new RangeExceptionImpl( RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) { throw new DOMException( DOMException.WRONG_DOCUMENT_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null)); } } checkIndex(refNode, offset); fStartContainer = refNode; fStartOffset = offset; // If one boundary-point of a Range is set to have a root container // other // than the current one for the Range, the Range should be collapsed to // the new position. // The start position of a Range should never be after the end position. if (getCommonAncestorContainer() == null || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) { collapse(true); } } public void setEnd(Node refNode, int offset) throws RangeException, DOMException { if (fDocument.errorChecking) { if (fDetach) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if ( !isLegalContainer(refNode)) { throw new RangeExceptionImpl( RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) { throw new DOMException( DOMException.WRONG_DOCUMENT_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null)); } } checkIndex(refNode, offset); fEndContainer = refNode; fEndOffset = offset; // If one boundary-point of a Range is set to have a root container // other // than the current one for the Range, the Range should be collapsed to // the new position. // The start position of a Range should never be after the end position. if (getCommonAncestorContainer() == null || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) { collapse(false); } } public void setStartBefore(Node refNode) throws RangeException { if (fDocument.errorChecking) { if (fDetach) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode) ) { throw new RangeExceptionImpl( RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) { throw new DOMException( DOMException.WRONG_DOCUMENT_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null)); } } fStartContainer = refNode.getParentNode(); int i = 0; for (Node n = refNode; n!=null; n = n.getPreviousSibling()) { i++; } fStartOffset = i-1; // If one boundary-point of a Range is set to have a root container // other // than the current one for the Range, the Range should be collapsed to // the new position. // The start position of a Range should never be after the end position. if (getCommonAncestorContainer() == null || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) { collapse(true); } } public void setStartAfter(Node refNode) throws RangeException { if (fDocument.errorChecking) { if (fDetach) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) { throw new RangeExceptionImpl( RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) { throw new DOMException( DOMException.WRONG_DOCUMENT_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null)); } } fStartContainer = refNode.getParentNode(); int i = 0; for (Node n = refNode; n!=null; n = n.getPreviousSibling()) { i++; } fStartOffset = i; // If one boundary-point of a Range is set to have a root container // other // than the current one for the Range, the Range should be collapsed to // the new position. // The start position of a Range should never be after the end position. if (getCommonAncestorContainer() == null || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) { collapse(true); } } public void setEndBefore(Node refNode) throws RangeException { if (fDocument.errorChecking) { if (fDetach) { throw new DOMException( DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if ( !hasLegalRootContainer(refNode) || !isLegalContainedNode(refNode)) { throw new RangeExceptionImpl( RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) { throw new DOMException( DOMException.WRONG_DOCUMENT_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null)); } } fEndContainer = refNode.getParentNode(); int i = 0; for (Node n = refNode; n!=null; n = n.getPreviousSibling()) { i++; } fEndOffset = i-1; // If one boundary-point of a Range is set to have a root container // other // than the current one for the Range, the Range should be collapsed to // the new position. // The start position of a Range should never be after the end position. if (getCommonAncestorContainer() == null || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) { collapse(false); } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?