org.w3c.dom.Text#getNextSibling ( )源码实例Demo

下面列出了org.w3c.dom.Text#getNextSibling ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk1.8-source-analysis   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码2 项目: TencentKona-8   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码3 项目: jdk8u60   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码4 项目: JDKSourceCode1.8   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码5 项目: openjdk-jdk8u   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码6 项目: openjdk-jdk8u-backup   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码7 项目: Bytecoder   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码8 项目: openjdk-jdk9   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码9 项目: hottub   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码10 项目: openjdk-8-source   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}
 
源代码11 项目: openjdk-8   文件: TextImpl.java
/**
 * 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 {
        this.setData(content);
        currentNode = this;
    }

    //check logically-adjacent text nodes
    Node prev = currentNode.getPreviousSibling();
    while (prev != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((prev.getNodeType() == Node.TEXT_NODE)
                || (prev.getNodeType() == Node.CDATA_SECTION_NODE)
                || (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(prev))) {
            parent.removeChild(prev);
            prev = currentNode;
        } else {
            break;
        }
        prev = prev.getPreviousSibling();
    }

    //check logically-adjacent text nodes
    Node next = currentNode.getNextSibling();
    while (next != null) {
        //If the logically-adjacent next node can be removed
        //remove it. A logically adjacent node can be removed if
        //it is a Text or CDATASection node or an EntityReference with
        //Text and CDATA only children.
        if ((next.getNodeType() == Node.TEXT_NODE)
                || (next.getNodeType() == Node.CDATA_SECTION_NODE)
                || (next.getNodeType() == Node.ENTITY_REFERENCE_NODE && hasTextOnlyChildren(next))) {
            parent.removeChild(next);
            next = currentNode;
        } else {
            break;
        }
        next = next.getNextSibling();
    }

    return currentNode;
}