Permalink
Browse files

rename simplifyText to coalesceText, and make it optionally descend.

  • Loading branch information...
1 parent 5eeee1c commit 45a58100611c66b2b4b4c1fdbe502faae349ab9c @rolfl rolfl committed Apr 5, 2012
Showing with 124 additions and 22 deletions.
  1. +13 −7 core/src/java/org/jdom2/Element.java
  2. +111 −15 test/src/java/org/jdom2/test/cases/TestElement.java
@@ -690,13 +690,17 @@ public Element setText(final String text) {
}
/**
- * Adjacent Text content is merged into the first Text in document
- * order, and the redundant Text items are removed (including any empty
- * Text).
+ * Adjacent Text content is merged into the first Text in document order,
+ * and the redundant Text items are removed (including any empty Text).
+ *
+ * @param recursively
+ * true if you want the text of child elements coalesced too. False
+ * if you only want to coalesce this Element's Text.
* @return true if any content was changed by this operation.
*/
- public boolean simplifyText() {
- final Iterator<Content> it = content.iterator();
+ public boolean coalesceText(boolean recursively) {
+ final Iterator<Content> it = recursively ? getDescendants()
+ : content.iterator();
Text tfirst = null;
boolean changed = false;
while (it.hasNext()) {
@@ -707,8 +711,10 @@ public boolean simplifyText() {
if ("".equals(text.getValue())) {
it.remove();
changed = true;
- } else if (tfirst == null) {
- // this could be the first of some adjacent text.
+ } else if (tfirst == null ||
+ tfirst.getParent() != text.getParent()) {
+ // previous item in the iterator was not text, or
+ // we are the next Text item after coming up the tree.
tfirst = text;
} else {
// add our text to the first in the sequence
@@ -3000,7 +3000,7 @@ public void testGetChildTextNormalizeElementStringNamespace() {
@Test
- public void testSimplifyTextSimple() {
+ public void testCoalesceTextSimple() {
Element root = new Element("root");
root.addContent("one");
root.addContent(" ");
@@ -3010,15 +3010,15 @@ public void testSimplifyTextSimple() {
assertTrue(5 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertTrue(root.simplifyText());
+ assertTrue(root.coalesceText(false));
assertTrue(1 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertFalse(root.simplifyText());
+ assertFalse(root.coalesceText(false));
assertEquals("one two three", root.getText());
}
@Test
- public void testSimplifyTextCDATA() {
+ public void testCoalesceTextCDATA() {
Element root = new Element("root");
root.addContent("one");
root.addContent(" ");
@@ -3028,15 +3028,15 @@ public void testSimplifyTextCDATA() {
assertTrue(5 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertTrue(root.simplifyText());
+ assertTrue(root.coalesceText(false));
assertTrue(3 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertFalse(root.simplifyText());
+ assertFalse(root.coalesceText(false));
assertEquals("one two three", root.getText());
}
@Test
- public void testSimplifyTextNested() {
+ public void testCoalesceTextNested() {
Element root = new Element("root");
root.addContent("one");
root.addContent(" ");
@@ -3048,15 +3048,15 @@ public void testSimplifyTextNested() {
assertTrue(5 == root.getContentSize());
assertEquals("one three", root.getText());
- assertTrue(root.simplifyText());
+ assertTrue(root.coalesceText(false));
assertTrue(3 == root.getContentSize());
assertEquals("one three", root.getText());
- assertFalse(root.simplifyText());
+ assertFalse(root.coalesceText(false));
assertEquals("one three", root.getText());
}
@Test
- public void testSimplifyTextEmpty() {
+ public void testCoalesceTextEmpty() {
Element root = new Element("root");
root.addContent("");
root.addContent("one");
@@ -3074,23 +3074,119 @@ public void testSimplifyTextEmpty() {
assertTrue(11 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertTrue(root.simplifyText());
+ assertTrue(root.coalesceText(false));
assertTrue(1 == root.getContentSize());
assertEquals("one two three", root.getText());
- assertFalse(root.simplifyText());
+ assertFalse(root.coalesceText(false));
assertEquals("one two three", root.getText());
}
@Test
- public void testSimplifyTextSingle() {
+ public void testCoalesceTextSingle() {
Element root = new Element("root");
root.addContent("");
assertEquals("", root.getText());
- assertTrue(root.simplifyText());
+ assertTrue(root.coalesceText(false));
assertTrue(0 == root.getContentSize());
assertEquals("", root.getText());
- assertFalse(root.simplifyText());
+ assertFalse(root.coalesceText(false));
+ assertEquals("", root.getText());
+ }
+
+
+ @Test
+ public void testCoalesceTextSimpleRec() {
+ Element root = new Element("root");
+ root.addContent("one");
+ root.addContent(" ");
+ root.addContent("two");
+ root.addContent(" ");
+ root.addContent("three");
+ assertTrue(5 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+
+ assertTrue(root.coalesceText(true));
+ assertTrue(1 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.coalesceText(true));
+ assertEquals("one two three", root.getText());
+ }
+
+ @Test
+ public void testCoalesceTextCDATARec() {
+ Element root = new Element("root");
+ root.addContent("one");
+ root.addContent(" ");
+ root.addContent(new CDATA("two"));
+ root.addContent(" ");
+ root.addContent("three");
+ assertTrue(5 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+
+ assertTrue(root.coalesceText(true));
+ assertTrue(3 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.coalesceText(true));
+ assertEquals("one two three", root.getText());
+ }
+
+ @Test
+ public void testCoalesceTextNestedRec() {
+ Element root = new Element("root");
+ root.addContent("one");
+ root.addContent(" ");
+ Element kid = new Element("kid");
+ root.addContent(kid);
+ kid.addContent("two");
+ root.addContent(" ");
+ root.addContent("three");
+ assertTrue(5 == root.getContentSize());
+ assertEquals("one three", root.getText());
+
+ assertTrue(root.coalesceText(true));
+ assertTrue(3 == root.getContentSize());
+ assertEquals("one three", root.getText());
+ assertFalse(root.coalesceText(true));
+ assertEquals("one three", root.getText());
+ }
+
+ @Test
+ public void testCoalesceTextEmptyRec() {
+ Element root = new Element("root");
+ root.addContent("");
+ root.addContent("one");
+ root.addContent("");
+ root.addContent(" ");
+ root.addContent("");
+
+ root.addContent("two");
+
+ root.addContent("");
+ root.addContent(" ");
+ root.addContent("");
+ root.addContent("three");
+ root.addContent("");
+ assertTrue(11 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+
+ assertTrue(root.coalesceText(true));
+ assertTrue(1 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.coalesceText(true));
+ assertEquals("one two three", root.getText());
+ }
+
+ @Test
+ public void testCoalesceTextSingleRec() {
+ Element root = new Element("root");
+ root.addContent("");
+ assertEquals("", root.getText());
+
+ assertTrue(root.coalesceText(true));
+ assertTrue(0 == root.getContentSize());
+ assertEquals("", root.getText());
+ assertFalse(root.coalesceText(true));
assertEquals("", root.getText());
}

0 comments on commit 45a5810

Please sign in to comment.