Java Code Examples for org.w3c.dom.Text#getPreviousSibling()

The following examples show how to use org.w3c.dom.Text#getPreviousSibling() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextImpl.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: TextImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: TextImpl.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: TextImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: TextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: TextImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: TextImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: TextImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: TextImpl.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source File: TextImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: TextImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}