nddelima 2004/11/08 14:21:39
Modified: java/tests/dom/dom3 Test.java
java/src/org/apache/xerces/dom TextImpl.java
Log:
Applying patches for getWholeText[1] and replaceWholeText[2] submitted by
Naela Nissar with minor modifications. Also fixing a bad getWholeText test
(dom.dom3.Test) that was broken by the fix.
[1] http://nagoya.apache.org/eyebrowse/[EMAIL PROTECTED]&msgNo=4736
[2] http://nagoya.apache.org/eyebrowse/[EMAIL PROTECTED]&msgNo=4737
Revision Changes Path
1.18 +1 -1 xml-xerces/java/tests/dom/dom3/Test.java
Index: Test.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/tests/dom/dom3/Test.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- Test.java 24 Feb 2004 23:47:22 -0000 1.17
+++ Test.java 8 Nov 2004 22:21:39 -0000 1.18
@@ -487,7 +487,7 @@
String compare1 = "Home Address: 1900 Dallas Road (East) City:
Dallas. California. USA PO #5668";
Assertion.verify(((TextImpl)ls.item(0)).getWholeText().equals(compare1),
"Compare1");
- String compare2 = "Address: 1900 Dallas Road (East) City:
Dallas. California. USA PO #5668";
+ String compare2 = "Home Address: 1900 Dallas Road (East) City:
Dallas. California. USA PO #5668";
Assertion.verify(((TextImpl)ls.item(1)).getWholeText().equals(compare2),
"Compare2");
1.29 +99 -11 xml-xerces/java/src/org/apache/xerces/dom/TextImpl.java
Index: TextImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/TextImpl.java,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -r1.28 -r1.29
--- TextImpl.java 4 Nov 2004 21:57:46 -0000 1.28
+++ TextImpl.java 8 Nov 2004 22:21:39 -0000 1.29
@@ -141,9 +141,7 @@
if (needsSyncData()) {
synchronizeData();
}
- if (nextSibling == null) {
- return data;
- }
+
if (fBufferStr == null){
fBufferStr = new StringBuffer();
}
@@ -153,26 +151,56 @@
if (data != null && data.length() != 0) {
fBufferStr.append(data);
}
- getWholeText(nextSibling, fBufferStr);
- return fBufferStr.toString();
+
+ //concatenate text of logically adjacent text nodes to the left of
this node in the tree
+ getWholeTextBackward(this.getPreviousSibling(), fBufferStr,
this.getParentNode());
+ String temp = fBufferStr.toString();
+
+ //clear buffer
+ fBufferStr.setLength(0);
+
+ //concatenate text of logically adjacent text nodes to the right of
this node in the tree
+ getWholeTextForward(this.getNextSibling(), fBufferStr,
this.getParentNode());
+
+ return temp + fBufferStr.toString();
}
-
+
/**
- * Concatenates the text of all logically-adjacent text nodes
+ * internal method taking a StringBuffer in parameter and inserts the
+ * text content at the start of the buffer
*
+ * @param buf
+ */
+ protected void insertTextContent(StringBuffer buf) throws DOMException {
+ String content = getNodeValue();
+ if (content != null) {
+ buf.insert(0, content);
+ }
+ }
+
+ /**
+ * Concatenates the text of all logically-adjacent text nodes to the
+ * right of this node
* @param node
* @param buffer
+ * @param parent
* @return true - if execution was stopped because the type of node
* other than EntityRef, Text, CDATA is encountered, otherwise
* return false
*/
- private boolean getWholeText(Node node, StringBuffer buffer){
- String text;
+ private boolean getWholeTextForward(Node node, StringBuffer buffer, Node
parent){
+ // boolean to indicate whether node is a child of an entity reference
+ boolean inEntRef = false;
+
+ if (parent!=null) {
+ inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE;
+ }
+
while (node != null) {
short type = node.getNodeType();
if (type == Node.ENTITY_REFERENCE_NODE) {
- if (getWholeText(node.getFirstChild(), buffer)){
+ if (getWholeTextForward(node.getFirstChild(), buffer, node)){
return true;
}
}
@@ -186,6 +214,62 @@
node = node.getNextSibling();
}
+
+ // if the parent node is an entity reference node, must
+ // check nodes to the right of the parent entity reference node for
logically adjacent
+ // text nodes
+ if (inEntRef) {
+ getWholeTextForward(parent.getNextSibling(), buffer,
parent.getParentNode());
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Concatenates the text of all logically-adjacent text nodes to the
left of
+ * the node
+ * @param node
+ * @param buffer
+ * @param parent
+ * @return true - if execution was stopped because the type of node
+ * other than EntityRef, Text, CDATA is encountered, otherwise
+ * return false
+ */
+ private boolean getWholeTextBackward(Node node, StringBuffer buffer,
Node parent){
+
+ // boolean to indicate whether node is a child of an entity reference
+ boolean inEntRef = false;
+ if (parent!=null) {
+ inEntRef = parent.getNodeType()==Node.ENTITY_REFERENCE_NODE;
+ }
+
+ while (node != null) {
+ short type = node.getNodeType();
+ if (type == Node.ENTITY_REFERENCE_NODE) {
+ if (getWholeTextBackward(node.getLastChild(), buffer, node)){
+ return true;
+ }
+ }
+ else if (type == Node.TEXT_NODE ||
+ type == Node.CDATA_SECTION_NODE) {
+ ((TextImpl)node).insertTextContent(buffer);
+ }
+ else {
+ return true;
+ }
+
+ node = node.getPreviousSibling();
+ }
+
+ // if the parent node is an entity reference node, must
+ // check nodes to the left of the parent entity reference node for
logically adjacent
+ // text nodes
+ if (inEntRef) {
+ getWholeTextBackward(parent.getPreviousSibling(), buffer,
parent.getParentNode());
+ return true;
+ }
+
return false;
}
@@ -262,6 +346,8 @@
|| (prev.getNodeType() == Node.ENTITY_REFERENCE_NODE &&
hasTextOnlyChildren(prev))) {
parent.removeChild(prev);
prev = currentNode;
+ } else {
+ break;
}
prev = prev.getPreviousSibling();
}
@@ -278,6 +364,8 @@
|| (next.getNodeType() == Node.ENTITY_REFERENCE_NODE &&
hasTextOnlyChildren(next))) {
parent.removeChild(next);
next = currentNode;
+ } else {
+ break;
}
next = next.getNextSibling();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]