下面列出了org.w3c.dom.Text#getPreviousSibling ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}