textimpl.java
来自「JAVA 所有包」· Java 代码 · 共 663 行 · 第 1/2 页
JAVA
663 行
/* * 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. */package com.sun.org.apache.xerces.internal.dom;import org.w3c.dom.CharacterData;import org.w3c.dom.DOMException;import org.w3c.dom.Node;import org.w3c.dom.Text;/** * Text nodes hold the non-markup, non-Entity content of * an Element or Attribute. * <P> * When a document is first made available to the DOM, there is only * one Text object for each block of adjacent plain-text. Users (ie, * applications) may create multiple adjacent Texts during editing -- * see {@link org.w3c.dom.Element#normalize} for discussion. * <P> * Note that CDATASection is a subclass of Text. This is conceptually * valid, since they're really just two different ways of quoting * characters when they're written out as part of an XML stream. * * @xerces.internal * * @version $Id: TextImpl.java,v 1.2.6.1 2005/08/31 12:46:37 sunithareddy Exp $ * @since PR-DOM-Level-1-19980818. */public class TextImpl extends CharacterDataImpl implements CharacterData, Text { // // Private Data members // // // Constants // /** Serialization version. */ static final long serialVersionUID = -5294980852957403469L; // // Constructors // /** Default constructor */ public TextImpl(){} /** Factory constructor. */ public TextImpl(CoreDocumentImpl ownerDoc, String data) { super(ownerDoc, data); } /** * NON-DOM: resets node and sets specified values for the current node * * @param ownerDoc * @param data */ public void setValues(CoreDocumentImpl ownerDoc, String data){ flags=0; nextSibling = null; previousSibling=null; setOwnerDocument(ownerDoc); super.data = data; } // // Node methods // /** * A short integer indicating what type of node this is. The named * constants for this value are defined in the org.w3c.dom.Node interface. */ public short getNodeType() { return Node.TEXT_NODE; } /** Returns the node name. */ public String getNodeName() { return "#text"; } /** * NON-DOM: Set whether this Text is ignorable whitespace. */ public void setIgnorableWhitespace(boolean ignore) { if (needsSyncData()) { synchronizeData(); } isIgnorableWhitespace(ignore); } // setIgnorableWhitespace(boolean) /** * DOM L3 Core CR - Experimental * * Returns whether this text node contains * element content whitespace</a>, often abusively called "ignorable whitespace". * The text node is determined to contain whitespace in element content * during the load of the document or if validation occurs while using * <code>Document.normalizeDocument()</code>. * @since DOM Level 3 */ public boolean isElementContentWhitespace() { // REVISIT: is this implemenation correct? if (needsSyncData()) { synchronizeData(); } return internalIsIgnorableWhitespace(); } /** * DOM Level 3 WD - Experimental. * Returns all text of <code>Text</code> nodes logically-adjacent text * nodes to this node, concatenated in document order. * @since DOM Level 3 */ public String getWholeText(){ if (needsSyncData()) { synchronizeData(); } if (fBufferStr == null){ fBufferStr = new StringBuffer(); } else { fBufferStr.setLength(0); } if (data != null && data.length() != 0) { fBufferStr.append(data); } //concatenate text of logically adjacent text nodes to the left of this node in the tree getWholeTextBackward(this.getPreviousSibling(), fBufferStr, this.getParentNode()); String temp = fBufferStr.toString(); //clear buffer fBufferStr.setLength(0); //concatenate text of logically adjacent text nodes to the right of this node in the tree getWholeTextForward(this.getNextSibling(), fBufferStr, this.getParentNode()); return temp + fBufferStr.toString(); } /** * internal method taking a StringBuffer in parameter and inserts the * text content at the start of the buffer * * @param buf */ protected void insertTextContent(StringBuffer buf) throws DOMException { String content = getNodeValue(); if (content != null) { buf.insert(0, content); } } /** * Concatenates the text of all logically-adjacent text nodes to the * right of this node * @param node * @param buffer * @param parent * @return true - if execution was stopped because the type of node * other than EntityRef, Text, CDATA is encountered, otherwise * return false */ private boolean getWholeTextForward(Node node, StringBuffer buffer, Node parent){ // boolean to indicate whether node is a child of an entity reference boolean inEntRef = false; if (parent!=null) { inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE; } while (node != null) { short type = node.getNodeType(); if (type == Node.ENTITY_REFERENCE_NODE) { if (getWholeTextForward(node.getFirstChild(), buffer, node)){ return true; } } else if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { ((NodeImpl)node).getTextContent(buffer); } else { return true; } node = node.getNextSibling(); } // if the parent node is an entity reference node, must // check nodes to the right of the parent entity reference node for logically adjacent // text nodes if (inEntRef) { getWholeTextForward(parent.getNextSibling(), buffer, parent.getParentNode()); return true; } return false; } /** * Concatenates the text of all logically-adjacent text nodes to the left of * the node * @param node * @param buffer * @param parent * @return true - if execution was stopped because the type of node * other than EntityRef, Text, CDATA is encountered, otherwise * return false */ private boolean getWholeTextBackward(Node node, StringBuffer buffer, Node parent){ // boolean to indicate whether node is a child of an entity reference boolean inEntRef = false; if (parent!=null) { inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE; } while (node != null) { short type = node.getNodeType(); if (type == Node.ENTITY_REFERENCE_NODE) { if (getWholeTextBackward(node.getLastChild(), buffer, node)){ return true; } } else if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) { ((TextImpl)node).insertTextContent(buffer); } else { return true; } node = node.getPreviousSibling(); } // if the parent node is an entity reference node, must // check nodes to the left of the parent entity reference node for logically adjacent // text nodes if (inEntRef) { getWholeTextBackward(parent.getPreviousSibling(), buffer, parent.getParentNode()); return true; } return false; } /** * Replaces the text of the current node and all logically-adjacent text * nodes with the specified text. All logically-adjacent text nodes are * removed including the current node unless it was the recipient of the * replacement text. * * @param content * The content of the replacing Text node. * @return text - The Text node created with the specified content. * @since DOM Level 3 */ public Text replaceWholeText(String content) throws DOMException { if (needsSyncData()) { synchronizeData(); } //if the content is null Node parent = this.getParentNode(); if (content == null || content.length() == 0) { // remove current node if (parent != null) { // check if node in the tree parent.removeChild(this); } return null; } // make sure we can make the replacement if (ownerDocument().errorChecking) { if (!canModifyPrev(this)) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null)); } // make sure we can make the replacement if (!canModifyNext(this)) { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null)); } } //replace the text node Text currentNode = null; if (isReadOnly()) { Text newNode = this.ownerDocument().createTextNode(content); if (parent != null) { // check if node in the tree parent.insertBefore(newNode, this); parent.removeChild(this); currentNode = newNode; } else { return newNode; } } else {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?