Permalink
Browse files

Add simplifyText method which merges adjacent Text content

  • Loading branch information...
rolfl committed Apr 3, 2012
1 parent 51f74a8 commit d4b5c67f1df51cacf05e704e238f1d5361828233
Showing with 142 additions and 0 deletions.
  1. +36 −0 core/src/java/org/jdom2/Element.java
  2. +106 −0 test/src/java/org/jdom2/test/cases/TestElement.java
@@ -686,6 +686,42 @@ public Element setText(final String text) {
return this;
}
+
+ /**
+ * Adjacent Text content is merged into the first Text in document
+ * order, and the redundant Text items are removed (including any empty
+ * Text).
+ * @return true if any content was changed by this operation.
+ */
+ public boolean simplifyText() {
+ final Iterator<Content> it = content.iterator();
+ Text tfirst = null;
+ boolean changed = false;
+ while (it.hasNext()) {
+ final Content c = it.next();
+ if (c.getCType() == CType.Text) {
+ // Text, and no CDATA!
+ final Text text = (Text)c;
+ if ("".equals(text.getValue())) {
+ it.remove();
+ changed = true;
+ } else if (tfirst == null) {
+ // this could be the first of some adjacent text.
+ tfirst = text;
+ } else {
+ // add our text to the first in the sequence
+ tfirst.append(text.getValue());
+ // remove us from the sequence
+ it.remove();
+ changed = true;
+ }
+ } else {
+ // the end of the sequence
+ tfirst = null;
+ }
+ }
+ return changed;
+ }
/**
* This returns the full content of the element as a List which
@@ -2878,6 +2878,19 @@ public int compare(Attribute o1, Attribute o2) {
checkAttOrder(emt.getAttributes(), att4, att5, att1, att2, att3);
}
+
+ @Test
+ public void testSortAttributesNone() {
+ Element emt = new Element("root");
+ emt.sortAttributes(new Comparator<Attribute>() {
+ @Override
+ public int compare(Attribute o1, Attribute o2) {
+ return - o1.getNamespaceURI().compareTo(o2.getNamespaceURI());
+ }
+ });
+ // this ends up creating the Attribute array inside the Element.
+ checkAttOrder(emt.getAttributes());
+ }
private void checkAttOrder(List<Attribute> attributes, Attribute...atts) {
assertTrue(atts.length == attributes.size());
@@ -2983,6 +2996,99 @@ public void testGetChildTextNormalizeElementStringNamespace() {
assertEquals("", root.getChildTextNormalize("kid", ns));
}
+
+ @Test
+ public void testSimplifyTextSimple() {
+ 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.simplifyText());
+ assertTrue(1 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.simplifyText());
+ assertEquals("one two three", root.getText());
+ }
+ @Test
+ public void testSimplifyTextCDATA() {
+ 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.simplifyText());
+ assertTrue(3 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.simplifyText());
+ assertEquals("one two three", root.getText());
+ }
+
+ @Test
+ public void testSimplifyTextNested() {
+ 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.simplifyText());
+ assertTrue(3 == root.getContentSize());
+ assertEquals("one three", root.getText());
+ assertFalse(root.simplifyText());
+ assertEquals("one three", root.getText());
+ }
+ @Test
+ public void testSimplifyTextEmpty() {
+ 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.simplifyText());
+ assertTrue(1 == root.getContentSize());
+ assertEquals("one two three", root.getText());
+ assertFalse(root.simplifyText());
+ assertEquals("one two three", root.getText());
+ }
+
+ @Test
+ public void testSimplifyTextSingle() {
+ Element root = new Element("root");
+ root.addContent("");
+ assertEquals("", root.getText());
+
+ assertTrue(root.simplifyText());
+ assertTrue(0 == root.getContentSize());
+ assertEquals("", root.getText());
+ assertFalse(root.simplifyText());
+ assertEquals("", root.getText());
+ }
}

0 comments on commit d4b5c67

Please sign in to comment.